Results 1 to 11 of 11
- 11-25-2009, 08:12 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
lazy loading of subclass in hibernate
I have a super class Disc and two child classes : AudioDisc and VideoDisc. I am trying both the strategy Union subclass and join Subclass but i could not find any way to lazy load the subClass.
My classes are something like this :
Now when i am callingJava Code:public class Reservation { private int id ; private Set<Disc>discs = new HashSet<Disc>(); public Set<Disc> getDiscs() { return discs; } public void setDiscs(Set<Disc> discs) { this.discs = discs; } public int getId() { return id; } public void setId(int id) { this.id = id; } } public class Disc { private int id ; public int getId() { return id; } public void setId(int id) { this.id = id; } } public class AudioDisc extends Disc{ private String singer ; private int numOfSongs ; public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public int getNumOfSongs() { return numOfSongs; } public void setNumOfSongs(int numOfSongs) { this.numOfSongs = numOfSongs; } } public class VideoDisc extends Disc{ private String director ; private String language ; public String getDirector() { return director; } public void setDirector(String director) { this.director = director; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } }
List<Reservation>reservations = query.list();
in main()
and calling getDiscs() overs each reservation object the sql is fired to join or union (based on the strategy defined in mapping) to fetch the data from DB. can we set the lazy loading of these subclass ?? i mean to say that the sql corresponding to that subclass should not be fired untill we have not accessed the property of the class . If it is not possible then what is the use of providing the lazy attribute in the metadata definition of joinsubclass or unionsubclass (which holds the default value "false").
Any help is appreciated .
Thanks.
- 11-25-2009, 09:17 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Did you mean "should not be fired untill we have accessed the property of the class"?
Are you iterating within a session?
- 11-25-2009, 09:27 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
I don't quite understand what you're getting at. You have subclasses, not attributes.
That is, as far as I can remember, lazy loading is all about not getting data from other tables until it is needed. So if your Disc objects had an attribute Artist, which referenced an artist table, then that would not be constructed until getArtist was called (presuming lazy loading was in place for it).
What you have here is not the same thing at all.
- 11-25-2009, 09:47 AM #4
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
Yes ! I am iterating in a session . what i mean to say is actually , when i am calling reservation.getDiscs() a sql is fired(Union or Join based on subclass strategy) to load the audioDisc and videoDisc. I don't want that. I want that audio disc should be loaded when i will call audioDisc.getSinger() or videoDisc is loaded when i call videoDisc.getDirector() .(for sure, i will call these getters after confirming that these getters can be appllied to this object instance.) . The all i want to say that i want to load the subClasses (AudioDisc and VideoDisc lazily).
Thanks.
- 11-25-2009, 09:53 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You can't.
Because it is a subclass.
Hibernate does your search, building the Disc objects. At that point in time it needs to know which class it is constructing. And it is at that point it gets all the data for that class, bar any associated data represented by another mapping.
Show us your hibernate xml for these classes, and their table mappings.
- 11-25-2009, 10:02 AM #6
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
OK.. I am agree . then why "lazy" property is provided in the meta-data definition of join-subclass . I could not get that . Could you please explain that.<hibernate-mapping>
<class name="Disc" table="DISC_PERSUBCLASS">
<id name="id" type="integer" column="ID" >
<generator class ="assigned"/>
</id>
<joined-subclass name="AudioDisc" lazy="true" table="AUDIODISC_PERSUBCLASS">
<key column="DISC_ID" />
<property name="singer" type="string" column="SINGER"/>
<property name="noOfSongs" type="int" column="NO_OF_SONGS"/>
</joined-subclass>
<joined-subclass name="VideoDisc" lazy="true" extends="Disc" table="VIDEODISC_PERSUBCLASS">
<key column="DISC_ID"></key>
<property name="director" type="string" column="DIRECTOR" />
<property name="language" type="string" column="LANG" />
</joined-subclass>
</class>
</hibernate-mapping>
Thanks.
- 11-25-2009, 10:50 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
No idea off hand.
I was abot to suggest asking over at the Hibernate forums, but I see you've done that. I expect you'll get a better answer over there.
Just in case, here's a link to the post over there. In case there's a better explanation than I've attempted to give.
- 11-25-2009, 11:07 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 11-25-2009, 11:47 AM #9
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
on a particularuse-case in my application, on query for reservation, i know upahead that all DISCs will be only 'AUDIO' type. There if i can get hibernate to not join to the VIDEODISC table, that will be great.
- 11-25-2009, 12:26 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
In which case write a specific query for that.
You are, essentially, expecting Hibernate to know in advance that the parameters it's being given for a query will not result in VideoDiscs being returned.
- 12-12-2009, 03:39 PM #11
Member
- Join Date
- Oct 2009
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
Lazy Initialization
By onegcr in forum New To JavaReplies: 1Last Post: 08-14-2007, 03:29 PM -
hibernate lazy=False
By Ed in forum JDBCReplies: 2Last Post: 07-02-2007, 07:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks