-->
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.  [ 4 posts ] 
Author Message
 Post subject: Strange problem with update (composite key)
PostPosted: Tue Jun 07, 2005 7:35 pm 
Newbie

Joined: Mon Jun 21, 2004 1:59 am
Posts: 4
Location: Sydney, Australia
Hi all,

I'm having a strange problem with updates (possibly due to a composite key) where I need to specifically call update() in order to persist changes to an object (I shouldn't need to).

The class mapping is defined as follows:

<class name="a.b.CompanyImpacts" table="CompanyPriorities">
<composite-id>
<key-property name="companyId" column="ExtCompanyId"/>
<key-property name="locationId" column="ExtLocationId"/>
</composite-id>
<property name="impacts" column="PriorityBitSet"/>
</class>

and the code looks something like:

CompanyImpacts ci = new CompanyImpacts();
ci.setCompanyId("abc");
ci.setLocationId("xyz");
s.get(CompanyImpacts.class, ci);
ci.setImpacts(56);

This code doesn't update the database (as I would have expected). I need to add an additional line of code:

s.update(ci);

... to get the update to work. This confuses me, as I understood that one only needed to explicitly call update for a long transaction (i.e. an object loaded from a separate session). Any ideas as to what the problem might be: lack of a native key?

Thanks,
Dominic.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 7:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Now, actually, i'm a little confused, since that *usually* works, even though it is not guaranteed to as part of the contract of get(). But probably there is something you aren't telling me (like, the session already had come CompanyImpact instances loaded). Anyway, the correct code is:



CompanyImpacts ci = new CompanyImpacts();
ci.setCompanyId("abc");
ci.setLocationId("xyz");
ci = (CompanyImpacts) s.get(CompanyImpacts.class, ci);
ci.setImpacts(56);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 7:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Another option is:
CompanyImpacts ci = new CompanyImpacts();
ci.setCompanyId("abc");
ci.setLocationId("xyz");
s.load(ci, ci);
ci.setImpacts(56);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 8:50 pm 
Newbie

Joined: Mon Jun 21, 2004 1:59 am
Posts: 4
Location: Sydney, Australia
O.k, I found the problem!

The thing is that I'm using Spring (sorry, forgot to mention) and didn't realise that the convenience methods such as getHibernateTemplate().get() are only for single step actions. By modifying an object after performing such an operation, you are already outside the session!

I did suspect something like this, but then tried to use an explicit call to getSession() to retrieve a session and then performed the operations within the context of that session. Trouble is, I didn't realise that you need to set up an Interceptor or use a call-back for this to work in Spring.

The thing that made all of this confusing was that the symptoms were the same in both cases: I could load data but not update it. The reasons, however, were quite different. Still, this experience made me understand how Hibernate works with Spring, especially in terms of thread-bound Sessions which are not obvious.

This stuff isn't clear from the "simple" example applications which seem to limit themselves to single step operations and hence make extensive use of getHibernateTemplate().find getHibernateTemplate().save, etc. I'm not sure how realistic this is, and might result in bad programming practices where developers are unnecessarily introducing long transactions.

Dominic.


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