Mapping Problem using hibernate and annotations

Hi,

i am German student and new to hibernate. I established a mn conetction between to entities using hbm.xml mapping files. now I try to create the same connection applying annotations. unfortunately it does not work and I do not now why. First my error message followed by my classes and xml's:

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

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")

publicclass Person{

privatelong id;

private String vorname;

private String nachname;

private List<Termin> termine=new LinkedList<Termin>();

public Person(){

}

@Id @GeneratedValue(strategy=GenerationType.AUTO)

publiclong getId(){

return id;

}

publicvoid setId(long id){

this.id = id;

}

public String getNachname(){

return nachname;

}

publicvoid setNachname(String nachname){

this.nachname = nachname;

}

public String getVorname(){

return vorname;

}

publicvoid setVorname(String vorname){

this.vorname = vorname;

}

publicvoid addTermin(Termin termin){

termine.add(termin);

}

@ManyToMany(mappedBy="person")

public List<Termin> getTermine(){

return termine;

}

publicvoid setTermine(List<Termin> termine){

this.termine = termine;

}

}

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")

publicclass Termin{

privatelong id;

private String titel;

private Person eigentuemer;

private List<Person> teilnehmer=new ArrayList<Person>();

publicvoid addTeilnehmer(Person person){

teilnehmer.add(person);

}

@ManyToMany

public List<Person> getTeilnehmer(){

return teilnehmer;

}

publicvoid setTeilnehmer(List<Person> teilnehmer){

this.teilnehmer = teilnehmer;

}

public Termin(){

}

@Id @GeneratedValue(strategy=GenerationType.AUTO)

publiclong getId(){

return id;

}

publicvoid setId(long id){

this.id = id;

}

public String getTitel(){

return titel;

}

publicvoid setTitel(String titel){

this.titel = titel;

}

public Person getEigentuemer(){

return eigentuemer;

}

publicvoid setEigentuemer(Person eigentuemer){

this.eigentuemer = eigentuemer;

}

}

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;

publicclass Test{

publicstaticvoid 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();

*/

}

}

package services;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

import domain.Person;

import domain.Termin;

publicclass HibernateUtil{

privatestaticboolean recreateDB =false;

publicstaticvoid setRecreateDB(boolean recreateDB){

HibernateUtil.recreateDB = recreateDB;

}

publicstatic SessionFactory getSessionFactory(){

if (sessionFactory ==null){

sessionFactory = sessionFactory("hibernate.cfg.xml");

}

return sessionFactory;

}

privatestatic SessionFactory sessionFactory =null;

privatestatic 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);

thrownew ExceptionInInitializerError(ex);

}

}

}

<?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>

[14583 byte] By [FT77a] at [2007-11-27 11:38:56]
# 1

The error message is pretty much telling you the problem:

mappedBy reference an unknown target entity property: domain.Termin.person in domain.Person.termine

This tells you that there's a mappedBy setting on the Person class's termine property annotation, and that the property it's referring to (the person property of the Termin class) doesn't exist.

@ManyToMany(mappedBy="person")

public List<Termin> getTermine() {

return termine;

}

If we have a look at the Termin class, indeed it has the following properties:

id

teilnehmer

titel

eigentuemer

And no person property. Remember, a property is defined by the existence of a get/set pair for the name. It's unrelated to the types returned by them and the private variables implementing them.

mappedBy has the equivalent effect to the inverse property in a hbm.xml mapping file - it defines which entity's property will cause the foreign key value to be updated (persisting the relationship to the database). From the context of your code, I'm guessing you really want the annotation to read:

@ManyToMany(mappedBy="teilnehmer")

dcmintera at 2007-7-29 17:23:07 > top of Java-index,Java Essentials,New To Java...