-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate newby question: n:m relation mapping
PostPosted: Thu Mar 29, 2007 12:10 pm 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Hi,

I am new to Hibernate and want to use this great framework in my work.
To play around with Hibernate, I've created 3 tables in my database. The layout of the tables is as follow:

Table Student:
-idStudent (primary key)
-Vorname
-Nachname

Table Student2Vorlesung:
-idZuordnung (primary key)
-FSStudent (foreign key, references idStudent in table Student)
-FSVorlesung (foreign key, references idVorlesung in table Vorlesung)

Table Vorlesung:
-idVorlesung (primary key)
-Name

Now I want to map the Students to the Vorlesungen. Because it is a n:m relation, the table Student2Vorlesung models this relation. Now I want to add a Vorlesung to a Student as shown below in the code section, but unfortunately this action fails :(
Can someone help me and tell what I do wrong?

Hibernate version: 3.2.2 GA

Mapping documents:

Vorlesung.hbm.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="hibernate.Vorlesung" table="Vorlesung">
        <id name="idVorlesung" column="idVorlesungD">
            <generator class="native"/>
        </id>
        <property name="Name"/>
</class>
</hibernate-mapping>


Student.hbm.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>

    <class name="hibernate.Student" table="Student">
        <id name="idStudent" column="idStudent">
            <generator class="native"/>
        </id>
        <property name="Vorname"/>
        <property name="Nachname"/>
       
        <set name="vorlesungen" table="Student2Vorlesung">
           <key column="idStudent"/>
           <many-to-many column="idVorlesung" class="hibernate.Vorlesung"/>
       </set>
   
    </class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Code:
Session session = InitSessionFactory.getInstance().getCurrentSession();
      session.beginTransaction();
      
      Student aStudent = (Student) session.load(Student.class, new Long(1));
      Vorlesung aVorlesung = (Vorlesung) session.load(Vorlesung.class, new Long(2));
      
      aStudent.getVorlesungen().add(aVorlesung);
      
      session.getTransaction().commit();


Full stack trace of any exception that occurs:

Code:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not load an entity: [hibernate.Student#1]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1865)
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
   at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3038)
   at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
   at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
   at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:98)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
   at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:836)
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:66)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
   at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:140)
   at hibernate.Student$$EnhancerByCGLIB$$e1f810d6.getVorlesungen(<generated>)
   at hibernate.TestExample.main(TestExample.java:41)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "student" does not exist
   at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1548)
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1316)
   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:191)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:351)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:255)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1778)
   at org.hibernate.loader.Loader.doQuery(Loader.java:662)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1851)
   ... 14 more



Name and version of the database you are using:PostgreSQL 8.2

The generated SQL (show_sql=true):
Hibernate: select student0_.idStudent as idStudent1_0_, student0_.Vorname as Vorname1_0_, student0_.Nachname as Nachname1_0_ from Student student0_ where student0_.idStudent=?

Thanks for your help and time!
Bye,
TMK


Top
 Profile  
 
 Post subject: Easy to solve
PostPosted: Wed Jun 20, 2007 6:15 pm 
Newbie

Joined: Wed Jun 20, 2007 6:11 pm
Posts: 1
Every time you get an "relation xyz not found message" it simply means postgresql does not find the item. Please add the schema to your configuration for example after table="...." add schema="public" or whatever...

Hope this helps you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.