-->
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.  [ 2 posts ] 
Author Message
 Post subject: Subclasses, proxies and lazy-loading - How can it be done?
PostPosted: Fri Jul 29, 2005 10:10 am 
Newbie

Joined: Wed Apr 14, 2004 4:58 am
Posts: 6
Hi,

assume you have the following mapping:

[code]
<hibernate-mapping default-access="field">
<class name="AbstractClass" abstract="true">
<id name="id">
<generator class="increment"/>
</id>
<union-subclass name="Concrete1" table="c1">
<property name="p1"/>
</union-subclass>
<union-subclass name="Concrete2" table="c2">
<property name="p2"/>
</union-subclass>
</class>
<class name="Test">
<id name="id">
<generator class="increment"/>
</id>
<many-to-one name="x" class="AbstractClass" column="ref"/>
</class>
</hibernate-mapping>
[/code]

When I fetch all objects of type "AbstractClass", i can test if they are of type "Concrete1" or "Concrete2" and then cast them.
When I fetch all objects of type "Test" and then access the attribute "x", the result is a CGLib Proxy which extends "AbstractClass". This is afaik a known problem which results from the fact that the type of the object is known only after it's queried.

Now my question is how to workaround this.
1. I could set "lazy=false" for the mapping of "AbstractClass". This worked out-of-the-box for me. But what if the abstract class (or onde of it's implementations) contains a maped collection? will this cause that these collections are always fetched or will this only cause that neither for AbstractClass nor for the Implementations a proxy will be creared?

2. Maybe i could disable lazy-loading of the many-to-one in "Test". I think this should cause that the related class (Concrete1 or Concrete2) will loaded together with Test.

The intereting thing is that when i set lazy=false for AbstractClass and then load "Test", the related Concreate class is loaded togehter with "Test". When i instead set lazy=false for the many-to-one, it has no effect at all.

So the first way (disabling lazy fetching of AbstractClass) is currently the only thing i found to make this work. Any idea how to do it better?

Thank you,
Michael.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 11:10 am 
Newbie

Joined: Wed Apr 14, 2004 4:58 am
Posts: 6
A different solution for my problem is to use Interfaces for both (the abstract class and it's subclasses: IAbstractClass, IConcrete1 and IConcrete2) and tell hibernate to use these (<class proxy="IAbstractClass" ...>...<union-subclass proxy="IConcrete1" ...>).
This causes Hibernate to return a proxy which implements all these interfaces.

The Result then can be casted either to IConcrete1 or to IConcrete 2. In fact it can be casted to both objects and "result instanceof Conrecte1" and "result instanceof Conrecte2" will both return true. So an additional getInstanceType should be added to the concrete classes to determine the instance type.

Michael.


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