-->
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.  [ 3 posts ] 
Author Message
 Post subject: Transitive Persistence Problems
PostPosted: Tue Mar 29, 2005 4:41 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:29 am
Posts: 26
Hibernate version: 2.1
Name and version of the database you are using: Firebird 1.5

I've tried to map a Device object that has a set of Feature objects (see below).

After that, I wrote code to create a Device and a Feature, add the Feature
to the Device's set and save the Device. The intent was to save the Feature
as well via transitive persistence.

My initial problem was that when I called device.getFeatures().add(feature), I
got a NullPointerException in device.getFeatures() because there was no set.
I added a line in the Device constructor to create a new HashSet() - should I
have had to?

Now my problem is with the transitive persistence part. With or without
the cascade="all" in the mapping for Device, if I try to only save the
Device, the SQL I get seems to show an attempt to INSERT the Device and
UPDATE the Feature. The UPDATE fails because the Feature is not yet in
the table. The UPDATE appears to correspond to the feature.setFeatureName() call.

WITHOUT the cascade="all", if I save both the Device and the Feature, the
SQL shows an INSERT for each object, plus an UPDATE of the Feature to set
its foreign key to the DEVICE table.

WITH the cascade="all", if I save both the Device and the Feature, I get
the INSERT for the Device and the update for the Feature as before.

I'm sure I'm doing something dumb - can anyone see what? Most of the
examples that I've seen with cascade="all" have been for bi-di
associations - that's not a requirement, is it?

Thanks,
-Jeff

-------------------------------------------------------------------------------

Mapping documents:

<hibernate-mapping>
<class name="domain.Device" table="DEVICE">
<id name="id" column="device_id">
<generator class="hilo"/>
</id>
<property name="model"/>
<property name="ipAddress"/>
<set name="features" lazy="true" cascade="all">
<key column="device_id"/>
<one-to-many class="domain.Feature"/>
</set>
</class>
</hibernate-mapping>

<hibernate-mapping >
<class name="domain.Feature" table="FEATURE">
<id name="id" column="feature_id">
<generator class="hilo"/>
</id>
<property name="featureName"/>
</class>
</hibernate-mapping>

-------------------------------------------------------------------------------

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

(I am using the Spring framework to wrap things in transactions, but I've
done this same thing directly through Hibernate with the same results as above.)

Device device = new Device();
device.setIpAddress("157.184.70.140");
device.setModel("Optra");

Feature feature = new Feature();
feature.setFeatureName("Color");

device.getFeatures().add(feature);

session.save(device);
//session.save(feature);

-------------------------------------------------------------------------------

The generated SQL (show_sql=true):

Hibernate: insert into DEVICE (model, ipAddress, device_id) values (?, ?, ?)
Hibernate: update FEATURE set featureName=? where feature_id=?

[/code]


Top
 Profile  
 
 Post subject: Transitive Persistence Problems
PostPosted: Wed Mar 30, 2005 6:55 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:29 am
Posts: 26
Apparently Hibernate 3.0 hides a multitude of sins :-)

I was able to get this to work somewhat for Hibernate 2.1, but had to move things around in the code, etc.

I was about to try 3.0 anyway, and it seems to handle this scenario happily, pretty much no matter what order I make the calls in.

-Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 30, 2005 7:18 pm 
Regular
Regular

Joined: Thu Apr 15, 2004 1:12 pm
Posts: 55
Have you read through the discussion of "parent-child" relationships in the Hibernate docs?

http://www.hibernate.org/hib_docs/refer ... arentchild

—ml—


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