-->
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: P*morphic query breaks if entity proxy already xist in sess
PostPosted: Fri Oct 26, 2007 5:33 am 
Newbie

Joined: Fri Oct 26, 2007 4:24 am
Posts: 2
Location: South Africa
Hi Guys
My hibernate version is 3.2.5. JBoss is 4.0.5. Java is jdk1.5.0_08.
I'm executing a polymorphic query and expect an Individual instead of the Party entity I'm getting. Party is the super class for Individual and Organisation. I'm using table-per-class-hierarchy.

I've found that the reason this occurs is because earlier in the transaction another query has the side effect of a proxy being created for a Party with the same identifier as for the Individual I'm asking for later. So hibernate returns it instead.

There are two workarounds;
1. Call session.clear(). Probably not a good idea, eh?
2. Move the call to retrieve the Individual to earlier in the transaction so that no Party proxy could possibly exist yet.

I've implemented the second one.

Now, my question is: is this a bug? I hope so, because it can be awkward if different developers work on different pieces of business logic that end up running in the same transaction and have to find out why the polymorphism isn't working. Not easy to debug. Implementing a workaround could mean interdependencies between pieces of business logic that otherwise wouldn't exist.

If this is currently expected behaviour, then I hope I've argued succesfully for why it should change :-).

Lastly, I searched for this scenario in any way I could think of and found nothing in the forums or through Google, so if this has come up before, I apologise. English is not my first language.

Comments will be greatly appreciated.

_________________
Johan Hoogenboezem


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 9:23 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Johan,

If I understand your issue correctly, you can try this:

Assuming you have an instance of DomesticCat [extends Cat] already loaded, and it is of type Cat, but the actual subclass is DomesticCat. You can re-load it using the correct subclass and you'll get a second proxy of the right type:

Code:
// cat loaded previously
DomesticCat dc = (DomesticCat) session.load(DomesticCat.class, cat.getId());


Now you have two instances pointed at the same row; note that == will not work! But that should not matter because you're using business equality, right? :)

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 9:24 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
And of course since this entity is already in the Session cache, Hibernate will not go to the database...

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 1:51 am 
Newbie

Joined: Fri Oct 26, 2007 4:24 am
Posts: 2
Location: South Africa
Chris, thanks for you contribution, but...

Quote:
You can re-load it using the correct subclass and you'll get a second proxy of the right type

This defeats the object of having a polymorhphic query in the first place. I don't want to have to touch this code every time I make changes to the class hierarchy.

Quote:
And of course since this entity is already in the Session cache, Hibernate will not go to the database

That's like saying: "At least you still have the other leg"

_________________
Johan Hoogenboezem


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.