Many to Many

Status
Nicht offen für weitere Antworten.
G

Gast2

Gast
Hallo,

ich versuche in Hibernate gereade eine manytomany beziehung
hier meine 2 klassen
Code:
@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;
	}

Code:
@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
Code:
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
 
G

Gast2

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


wenn ich in dem hibernate.cfg.xml,das hier einfüge...
Code:
<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 =)

Code:
    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 ?????
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen


Oben