-->
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: Help needed on many-to-many implementation!!
PostPosted: Fri Sep 23, 2005 10:55 am 
Newbie

Joined: Thu Aug 18, 2005 12:24 pm
Posts: 11
Hibernate version: 3.1

I have a many-to-many schema ( students and courses ).

Hi. Im getting this exception when trying to list all students:

org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of br.com.mindsatwork.libraryweb.hibernatebeans.Student.setCourses
at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:217)
at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:187)
at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2945)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:115)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:791)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:221)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1610)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:117)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:102)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2476)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:358)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:339)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:116)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:78)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:649)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:85)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:131)
at br.com.mindsatwork.libraryweb.hibernatebeans.Student$$EnhancerByCGLIB$$11dd15c0.getFirstName(<generated>)
....

My classes are Student and Course. It is a many-to-many schema and my mapping file for classes are :

Student.hbm.xml :

<hibernate-mapping package="xx.xxx.xxxxxxxxx.libraryweb.hibernatebeans">

<class name="Student" table="student">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>

<property name="firstName" column="first_name" type="java.lang.String" />
<property name="lastName" column="last_name" type="java.lang.String" />
<property name="address" column="address" type="java.lang.String" />
<property name="contact" column="contact" type="java.lang.String" />

<set name="courses" table="college_registrations" inverse="true" cascade="all">
<key column="student_id"/>
<many-to-many column="course_id" class="Course" />
</set>
</class>

</hibernate-mapping>

Couse.hbm.xml :

<hibernate-mapping package="xx.xxx.xxxxxxxxx.libraryweb.hibernatebeans">
<class name="Course" table="course">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>

<set name="students" table="college_registrations" cascade="all" >
<key column="course_id"/>
<many-to-many column="student_id" class="Student" />
</set>



<property name="name" column="name" type="java.lang.String" />
<property name="prerequisites" column="prerequisites" type="java.lang.String" />
<property name="description" column="description" type="java.lang.String" />
<property name="numberOfCredits" column="credits" type="java.lang.Integer" />
</class>

</hibernate-mapping>

I have only one student and one course at database. The student is registered in the course ( wich is represented by a entry in college_registrations table that maps the many-to-many relation )

When I try to list the students I got the exception below...
Any Idea? Student class has a List of couses inside ( List getCourses, void setCourses( List courses ) ) and Couse class has a List of Students... )

Any Idea? HELP!
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 12:15 pm 
Newbie

Joined: Thu Aug 18, 2005 12:24 pm
Posts: 11
THE ERROR HAVE BEEN SOLVED! THANKS AND SORRY FOR THE INCONVENIENCE ;)


Top
 Profile  
 
 Post subject: I have a similar problem
PostPosted: Fri Sep 30, 2005 2:33 pm 
Newbie

Joined: Sun Aug 01, 2004 9:57 am
Posts: 4
I have a similar problem, my mappings are like these:

Categories:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="com.artconstructionsystems.dblayer.vobjects.Category" table="category">
      <id name="id" column="category_id" type="long">
         <generator class="native"/>
      </id>
      <property name="name"/>
      <many-to-one name="parentCategory" class="com.artconstructionsystems.dblayer.vobjects.Category" column="parent_id" cascade="all"/>
      <set name="childCategories" table="category" cascade="all" inverse="true">
         <key column="parent_id"/>
         <one-to-many class="com.artconstructionsystems.dblayer.vobjects.Category"/>
      </set>
      <set name="items" table="category_item" cascade="save-update">
         <key column="category_id"/>
         <many-to-many class="com.artconstructionsystems.dblayer.vobjects.Item" column="item_id"/>
      </set>
      
   </class>
</hibernate-mapping>


Items:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="com.artconstructionsystems.dblayer.vobjects.Item" table="item">
      <id name="id" column="item_id" type="long">
         <generator class="native"/>
      </id>
      <property name="itemName" type="text" not-null="true"/>
      <property name="createDate" type="java.util.Date" not-null="true"/>
      <!-- inverse end -->
      <set name="categories" table="category_item" inverse="true" cascade="save-update">
         <key column="item_id"/>
         <many-to-many class="com.artconstructionsystems.dblayer.vobjects.Category" column="category_id"/>
      </set>
   </class>
</hibernate-mapping>


When I delete an item I still have a reference to the category in the category_item table, how to remove the reference in table category_item!!??

I have tried delete-orphan but deletes the orphans in the category table.

Regards

totopo


Top
 Profile  
 
 Post subject: Query
PostPosted: Fri Sep 30, 2005 2:57 pm 
Newbie

Joined: Sun Aug 01, 2004 9:57 am
Posts: 4
and when I try to get all data from table Category using the HQL:

Code:
from Category c, order by category_id,parent_id,name


Code:
Hibernate: select item0_.item_id as item_id0_, item0_.itemName as itemName0_, item0_.createDate as createDate0_ from item item0_ where item0_.item_id=?


Code:
org.springframework.orm.hibernate.HibernateObjectRetrievalFailureException: No row with the given identifier exists: 1, of class: com.artconstructionsystems.dblayer.vobjects.Item; nested exception is net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: 1, of class: com.artconstructionsystems.dblayer.vobjects.Item
net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: 1, of class: com.artconstructionsystems.dblayer.vobjects.Item
   at net.sf.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:38)
   at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1964)
   at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:69)
   at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:208)
   at net.sf.hibernate.type.EntityType.nullSafeGet(EntityType.java:130)
   at net.sf.hibernate.collection.AbstractCollectionPersister.readElement(AbstractCollectionPersister.java:365)
   at net.sf.hibernate.collection.Set.readFrom(Set.java:233)
   at net.sf.hibernate.loader.Loader.readCollectionElement(Loader.java:384)
   at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:240)
   at net.sf.hibernate.loader.Loader.doQuery(Loader.java:285)
   at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
   at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:1020)
   at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:995)
   at net.sf.hibernate.loader.CollectionLoader.initialize(CollectionLoader.java:83)
   at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:288)
   at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3315)
   at net.sf.hibernate.collection.PersistentCollection.forceInitialization(PersistentCollection.java:336)
   at net.sf.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:3168)
   at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:143)
   at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
   at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
   at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
   at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:49)
   at org.springframework.orm.hibernate.HibernateTemplate$19.doInHibernate(HibernateTemplate.java:647)
   at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:314)
   at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:633)
   at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:612)
   at com.artconstructionsystems.dblayer.persistance.DBCategories.getCategories(DBCategories.java:57)
   at com.artconstructionsystems.dblayer.AppTest.testRetrieveCategories(AppTest.java:121)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)





or how could I impelement the next query using HQL?

Code:
select * from category,item,category_item where category.category_id = category_item.category_id and category_item.item_id = item.item_id


Please help

Regards

Marco


Top
 Profile  
 
 Post subject: was trivial
PostPosted: Sat Oct 01, 2005 5:09 pm 
Newbie

Joined: Sun Aug 01, 2004 9:57 am
Posts: 4
Ok, was trivial

inverse="true" was on the Category mapping.


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.