-->
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: problems on setting a parent for a child object
PostPosted: Wed Oct 19, 2005 6:12 am 
Newbie

Joined: Wed Oct 19, 2005 6:05 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
hibernate 3.1
Mapping documents:

<class name="Location">
<id name="id" unsaved-value="0" >
<generator class="hilo">
<param name="max_lo">0</param>
</generator>
</id>
<property name="no" />
<property name="ad" length="50"/>
<property name="description" length="255"/>

<many-to-one name="parent" class="Location" />

<list name="children" >
<key column="parent"/>
<index column="no" type="int"/>
<one-to-many class="Location" />
</list>


<list name="categories" lazy="true" >
<key column="category"/>
<index column="no" type="int"/>
<many-to-many class="Location"/>
</list>


</class>

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


Location mother= new Location("mother location");
sess.save(mother);

Location child = new Location("child location");
child.setParent(mother);

sess.save(child);

Full stack trace of any exception that occurs:
3640 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete
3687 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:sybase:Tds:taciz:4100/sandbox
3687 [main] INFO org.hibernate.impl.SessionFactoryImpl - Checking 0 named queries
Exception in thread "main" org.hibernate.PropertyAccessException: exception getting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) getter of akkam.sky.core.Location.?
at org.hibernate.tuple.PojoEntityTuplizer.getPropertyValuesWithOptimizer(PojoEntityTuplizer.java:229)
at org.hibernate.tuple.PojoEntityTuplizer.getPropertyValuesToInsert(PojoEntityTuplizer.java:203)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:3303)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:244)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:513)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:509)
at akkam.sky.Main.main(Main.java:52)
Caused by: java.lang.NullPointerException
at akkam.sky.core.Location.getNo(Location.java:75)
at akkam.sky.core.Location$$BulkBeanByCGLIB$$f465456b.getPropertyValues(<generated>)
at net.sf.cglib.beans.BulkBean.getPropertyValues(BulkBean.java:48)
at org.hibernate.tuple.PojoEntityTuplizer.getPropertyValuesWithOptimizer(PojoEntityTuplizer.java:226)
... 14 more
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

Name and version of the database you are using:
sybase any where

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: You should manage the collection
PostPosted: Wed Oct 19, 2005 3:08 pm 
Newbie

Joined: Wed Sep 17, 2003 3:31 am
Posts: 10
Location: Napoli (IT)
You should manage the collection. In this case you have to assign a value to the index in property 'Location.no'.

In order to keep the List 'Location.categories' in sync with the parent child association you also have to set 'no' to the same index. The simplest way to do this is setting the parent with an addChild() method:

Code:

public void addChild(Location loc)
{
    categories.add(loc);
    loc.setNo(categories.size() -1);
}



To avoid unnecessary SQL commands declare the collection as inverse. The manual has a detailed explanation and a lot of examples about this.


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.