-->
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.  [ 6 posts ] 
Author Message
 Post subject: Bug with inheritance
PostPosted: Wed Aug 23, 2006 5:40 am 
Newbie

Joined: Wed Aug 23, 2006 5:29 am
Posts: 3
Location: Zurich
[b]Hibernate version: JBoss EJB-3RC8[/b]

I have a simple class hierarchy that uses joined inheritance:
[code]
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class MaterialMaster {
private String externalId;
}

@Entity
@PrimaryKeyJoinColumn(name="ID")
public class SparePartMaster extends MaterialMaster {

}
[/code]

This hierarchy is referenced by another class:
[code]
@Entity
public class RecallOrderLine {
@ManyToOne(fetch=FetchType.LAZY, optional=false)
private RecallOrder recallOrder;

@OneToOne(fetch=FetchType.LAZY, optional=false)
private MaterialMaster material;
}
[/code]

When I query
[code]
SELECT r FROM RecallOrderLine r
WHERE r.recallOrder=:recallOrder
AND r.material =:material
[/code]
the returned RecallOrderLine has a SparePartMaster instance as expected.

Now when I query
[code]
SELECT r FROM RecallOrderLine r
WHERE r.recallOrder=:recallOrder
AND r.material.externalId =:matExtKey
[/code]
the returned RecallOrderLine has a MaterialMaster instance only!

My DB is correct. So it must be a bug in Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 8:24 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
It is just because of @OneToOne(fetch=FetchType.LAZY, optional=false).

When fetching an Entity in Lazy mode a proxy of the referenced entity is returned. So in your case a proxy for MaterialMaster will be created (which is a subclass of MaterialMaster). So that proxy could not be SparePartMaster.

I believe the best way to solve this problem is to set fetch=FetchType.EAGER.
------------------------

don't forget credits

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 8:46 am 
Newbie

Joined: Wed Aug 23, 2006 5:29 am
Posts: 3
Location: Zurich
Okay, thanks. I understand now, why it's doing that.

It's quite a stupid trap to fall in. When I decide to add a discriminator column to MaterialMaster to designate the type explicitely, then Hibernate definitely has the possibility to provide the correct subclass. Do you know if it does that?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 6:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This has nothing to do with the discriminator column and you get different results because in the second case you do not have the material instance in your persistence context. use @LazyToOne(NO_PROXY) and enhance your classes if you can't live wo using instanceof

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 4:37 am 
Newbie

Joined: Wed Aug 23, 2006 5:29 am
Posts: 3
Location: Zurich
Thanks emmanuel. Stupid question: how do I enable byte code enhancement for this class? The LazyToOne doesn't seem to work out of the box.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 6:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
chek the hibernate reference documentation for buildtime enhancement, there is a descrimtion of the process

_________________
Emmanuel


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