Hallo,
ich lerne gerade den Einsatz von Hibernate. Ich habe bereits erfolgreich eine NM Beziehung zwischen Entitäten realisiert. Hierfür setzte ich *.hbm.xml mappingfiles ein. Nun möchte ich das gleiche Ergebniss mit Annotationen erreichen. Das klappt leider nicht. Zu erst die Fehlermeldung und dann meine Klassen und xml's:
Quote:
Initial SessionFactory creation failed.org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: domain.Termin.person in domain.Person.termine
Exception in thread "main" java.lang.ExceptionInInitializerError
at services.HibernateUtil.sessionFactory(HibernateUtil.java:39)
at services.HibernateUtil.getSessionFactory(HibernateUtil.java:20)
at test.Test.main(Test.java:20)
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: domain.Termin.person in domain.Person.termine
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:552)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:517)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:316)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
at services.HibernateUtil.sessionFactory(HibernateUtil.java:36)
... 2 more
Code:
package domain;
import java.util.LinkedList;
import java.util.List;
import domain.Termin;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "PERSON")
public class Person {
private long id;
private String vorname;
private String nachname;
private List<Termin> termine=new LinkedList<Termin>();
public Person() {
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNachname() {
return nachname;
}
public void setNachname(String nachname) {
this.nachname = nachname;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public void addTermin(Termin termin){
termine.add(termin);
}
@ManyToMany(mappedBy="person")
public List<Termin> getTermine() {
return termine;
}
public void setTermine(List<Termin> termine) {
this.termine = termine;
}
}
Code:
package domain;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "TERMIN")
public class Termin {
private long id;
private String titel;
private Person eigentuemer;
private List<Person> teilnehmer= new ArrayList<Person>();
public void addTeilnehmer(Person person){
teilnehmer.add(person);
}
@ManyToMany
public List<Person> getTeilnehmer() {
return teilnehmer;
}
public void setTeilnehmer(List<Person> teilnehmer) {
this.teilnehmer = teilnehmer;
}
public Termin() {
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitel() {
return titel;
}
public void setTitel(String titel) {
this.titel = titel;
}
public Person getEigentuemer() {
return eigentuemer;
}
public void setEigentuemer(Person eigentuemer) {
this.eigentuemer = eigentuemer;
}
}
Code:
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import services.HibernateUtil;
import domain.Person;
import domain.Termin;
public class Test {
public static void main(String[] args) {
Session session = null;
HibernateUtil.setRecreateDB(true);
session = HibernateUtil.getSessionFactory().getCurrentSession();
/* Person person1 =new Person();
person1.setNachname("P1");
Transaction transaction = session.beginTransaction();
session.save(person1);
transaction.commit();
Person person2 =new Person();
person2.setNachname("P2");
session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
session.save(person2);
transaction.commit();
Termin termin1 =new Termin();
termin1.setTitel("T1");
termin1.setEigentuemer(person1);
termin1.addTeilnehmer(person1);
termin1.addTeilnehmer(person2);
session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
session.save(termin1);
transaction.commit();
Termin termin2 =new Termin();
termin2.setTitel("t2");
termin2.setEigentuemer(person1);
termin2.addTeilnehmer(person1);
termin2.addTeilnehmer(person2);
transaction = session.beginTransaction();
session.save(termin2);
transaction.commit();
session.close();
*/
}
}
Code:
package services;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import domain.Person;
import domain.Termin;
public class HibernateUtil {
private static boolean recreateDB = false;
public static void setRecreateDB(boolean recreateDB) {
HibernateUtil.recreateDB = recreateDB;
}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
sessionFactory = sessionFactory("hibernate.cfg.xml");
}
return sessionFactory;
}
private static SessionFactory sessionFactory = null;
private static SessionFactory sessionFactory(String configurationFileName) {
try {
AnnotationConfiguration annotationConfiguration =
new AnnotationConfiguration()
.addAnnotatedClass(Person.class)
.addAnnotatedClass(Termin.class);
if (recreateDB) annotationConfiguration.setProperty("hibernate.hbm2ddl.auto", "create");
annotationConfiguration.configure();
return annotationConfiguration.buildSessionFactory();
} catch (Throwable ex){
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">application</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">application</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>
Vielen Dank für eure Hilfe
Felix