-->
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: Hibernate wont issue an UPDATE always an INSERT
PostPosted: Wed May 05, 2004 5:48 pm 
Beginner
Beginner

Joined: Tue Apr 06, 2004 6:13 pm
Posts: 39
Location: Pune, Maharashtra, INDIA
Hi,

In code, I issue a a get on an Hydrated object and display it to screen.
Transaction 1 over.

When the user changes some data and clicks save (I guess a Hibernate Transaction 2, disjoint from 1 is stared), I expected Hibernate to issue an UPDATE but it issues an INSERT. (Resulting in redundant data in DB)

I cannot understand why?
The UI ultimately calls a store() on the hydrated object, within which I issue a saveOrUpdate(this).

Any clues ?

PS:
I have tried to word my problem in simple terms.
The Code however is much elaborate than what I have described, UI calls a EJB layer, which gets data from a DOMAIN (hydrated) layer and ...

Table structure too is pretty complex (Couple of Many-to-One relation ships between 3 tables) but I feel that is not why I am getting this problem.

_________________
thks
G1


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 05, 2004 5:57 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
what is your generator strategy? Typically if your id is not null, then hibernate issues an update, if it is null, hibernate will gen key, and do an insert...
do you have "unsaved value" in your map?

James


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 05, 2004 6:09 pm 
Beginner
Beginner

Joined: Tue Apr 06, 2004 6:13 pm
Posts: 39
Location: Pune, Maharashtra, INDIA
My hibernate mappings are:

Extract of the hbm.xml files
Quote:
<hibernate-mapping>
<class name="equicom.offline.transmission_profile.TransmissionHosts" table="TRANSMISSION_HOSTS" dynamic-update="false" dynamic-insert="false">

<id name="hostID" column="HOST_ID" type="integer" unsaved-value="null">
<generator class="native"></generator>
</id>

<hibernate-mapping>
<class name="equicom.offline.transmission_profile.TransmissionCredentials" table="TRANSMISSION_CREDENTIALS" dynamic-update="false" dynamic-insert="false">

<id name="credId" column="CREDENTIAL_ID" type="integer" unsaved-value="null">
<generator class="native"></generator>
</id>

<hibernate-mapping>
<class name="equicom.offline.transmission_profile.TransmissionProfile" table="TRANSMISSION_PROFILE" dynamic-update="false" dynamic-insert="false">

<id name="profileID" column="PROFILE_ID" type="long" unsaved-value="null">
<generator class="native"></generator>
</id>

TransmissionProfile (many map to one) TransmissionCredentials (many map to one) TransmissionHosts

In the EJB layer, saveTransmissionProfile()...

I do a fetch of TransmissionProfile for a particular key. (say object existingTP).

Then create a new instance of TransmissionProfile (newTP = new TransmissionProfile())
set the attributes,
if (existingTP != null)
newTP.profileID = existingTP.profileID
//set the rest of the attributes as received from UI layer
newTP.attrib1 = fromUI.attrib1
...

//and call
newTP.store();

TransmissionProfile.store() contains a
saveOrUpdate(this);

_________________
thks
G1


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 06, 2004 10:43 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
can you stop in a debugger, and see if the value of your identifier is null?
If it is not null, hibernate will think it's an update.

James


Top
 Profile  
 
 Post subject: problem solved.
PostPosted: Thu May 06, 2004 2:04 pm 
Beginner
Beginner

Joined: Tue Apr 06, 2004 6:13 pm
Posts: 39
Location: Pune, Maharashtra, INDIA
My problem solved.
I was doing this,

My UI made a EJB call setProfileData(dataFromScreen)
In the EJB, I did a getFromDatabase() - hibernate call to determine if data already existed.

Then called a private mapIT() method, in which I created a new TransmissionProfile() and other logically-related Hibernate Objects.

If data already existed in DB, set the ID, attribute of the hydrated objects with the one just fetched and then set other attributes of the hydrated objects with those from UI.

Then called the saveOrUpdate(this) method on the hydrated objects.

Hibernate always issued an INSERT!

I made the following change:
In the EJB, setProfileData(dataFromScreen), did the getFromDatabase().

And in that object
(if it was NOT null) set the (NON identifier) values received from UI
(if it was NULL), created NEW hydrated objects, and set in it values received from UI

called the saveOrUpdate(this) method on the hydrated objects.

Hibernate always issued an UPDATE or INSERT as appropriate!

So I guess one cannot achieve an UPDATE simply by setting the ID (or primary key) to a known value of the hydrated object.

Any comments ?

_________________
thks
G1


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.