# Many to Many



## Gast2 (29. Dez 2008)

Hallo,

ich versuche in Hibernate gereade eine manytomany beziehung
hier meine 2 klassen

```
@Entity@Table(name = "EVENT")  
public class Event {
	
	private List<Person> person;
	
	@ManyToMany(mappedBy = "event",
			targetEntity=Event.class)
	public List<Person> getPerson() {
		return person;
	}
	public void setPerson(List<Person> person) {
		this.person = person;
	}
```


```
@Entity@Table(name="PERSON")  
public class Person {
	@ManyToMany( targetEntity=event.Event.class,
			cascade={CascadeType.PERSIST, CascadeType.MERGE})
	@JoinTable(
            name="PERSON_EVENT",
            joinColumns = @JoinColumn( name="person_id"),
            inverseJoinColumns = @JoinColumn( name="event_id"))
    @OrderBy("title desc")
    public List<Event> getEvents() {
		return events;
	}
```

Was ist daran falsch??
Ich bekomme folgende Exception

```
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: event.Event.event in event.Event.person
	at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:578)
	at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:543)
	at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1163)
	at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:329)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
	at util.HibernateUtil.<clinit>(HibernateUtil.java:13)
	at test.TestMain.main(TestMain.java:22)
Initial SessionFactory creation failed.org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: event.Event.event in event.Event.person
Exception in thread "main" java.lang.ExceptionInInitializerError
	at util.HibernateUtil.<clinit>(HibernateUtil.java:18)
	at test.TestMain.main(TestMain.java:22)
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: event.Event.event in event.Event.person
	at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:578)
	at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:543)
	at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1163)
	at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:329)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
	at util.HibernateUtil.<clinit>(HibernateUtil.java:13)
	... 1 more
```


----------



## foobar (29. Dez 2008)

Mach mal die targetEntity raus, die brauchste net.


----------



## Gast2 (29. Dez 2008)

okay hat geklappt...
für was genau brauch ich das mappedBy???


wenn ich in dem hibernate.cfg.xml,das hier einfüge...

```
<property name="hbm2ddl.auto">create</property>
```
sollte er doch mit die DB neu anlegen und die alten einträge raus machen oder???

so meine dritte frage =)


```
private void addEventToPerson(Long personId, Long eventId) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Person aPerson = (Person) session.load(Person.class, personId);
        Event anEvent = (Event) session.load(Event.class, eventId);
        anEvent.getPersonen().add(aPerson);
        session.getTransaction().commit();
        
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Event event = (Event) session.load(Event.class, eventId);
        System.out.println(event.getPersonen().size());
    }
```


warum kommt hier 0 raus und nicht 1 ?????


----------

