-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: mappedBy reference an unknown target entity property
PostPosted: Tue Jul 24, 2007 9:11 am 
Newbie

Joined: Wed Jun 06, 2007 8:09 am
Posts: 14
Location: Germany
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 24, 2007 11:16 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Hi Felix,
Termin hat keine Property namens person. Du musst folgendes angeben:

@ManyToMany(mappedBy="teilnehmer")

mappedBy gibt an, wie die Backreferenz im Referenzierten Objekt heisst.

Gruß,
Michael

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject: .MappingException: Could not determine type for:
PostPosted: Tue Jul 24, 2007 11:31 am 
Newbie

Joined: Wed Jun 06, 2007 8:09 am
Posts: 14
Location: Germany
Hallo,

danke für deinen Tip. Erhalte habe immer noch eine Fehlermeldung:
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="teilnehmer")
   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;
   }
   
}


Quote:
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: domain.Person, for columns: [org.hibernate.mapping.Column(eigentuemer)]
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.MappingException: Could not determine type for: domain.Person, for columns: [org.hibernate.mapping.Column(eigentuemer)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.Property.isValid(Property.java:185)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:440)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
at services.HibernateUtil.sessionFactory(HibernateUtil.java:36)
... 2 more


Was nun ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:09 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Du hast Termin#getEigentuemer() nicht gemappt. Entweder mit @Transient (dann wird das Property ignoriert) oder mit @ManyToOne annotieren.

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject: danke
PostPosted: Wed Jul 25, 2007 7:05 am 
Newbie

Joined: Wed Jun 06, 2007 8:09 am
Posts: 14
Location: Germany
es funktioniert. Danke!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.