Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-25-2009, 08:12 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
ganesh.guddu is on a distinguished road
Default 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 :
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;
}

}
Now when i am calling
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-25-2009, 09:17 AM
Senior Member
 
Join Date: Aug 2009
Posts: 2,192
Rep Power: 4
r035198x is on a distinguished road
Default
Did you mean "should not be fired untill we have accessed the property of the class"?
Are you iterating within a session?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-25-2009, 09:27 AM
Senior Member
 
Join Date: Apr 2009
Posts: 2,145
Rep Power: 4
Tolls will become famous soon enough
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-25-2009, 09:47 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
ganesh.guddu is on a distinguished road
Default
Originally Posted by r035198x View Post
Did you mean "should not be fired untill we have accessed the property of the class"?
Are you iterating within a session?
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-25-2009, 09:53 AM
Senior Member
 
Join Date: Apr 2009
Posts: 2,145
Rep Power: 4
Tolls will become famous soon enough
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-25-2009, 10:02 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
ganesh.guddu is on a distinguished road
Default
Quote:
<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>
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.

Thanks.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-25-2009, 10:50 AM
Senior Member
 
Join Date: Apr 2009
Posts: 2,145
Rep Power: 4
Tolls will become famous soon enough
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-25-2009, 11:07 AM
Senior Member
 
Join Date: Aug 2009
Posts: 2,192
Rep Power: 4
r035198x is on a distinguished road
Default
https://forum.hibernate.org/viewtopic.php?f=1&t=990657
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-25-2009, 11:47 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
ganesh.guddu is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-25-2009, 12:26 PM
Senior Member
 
Join Date: Apr 2009
Posts: 2,145
Rep Power: 4
Tolls will become famous soon enough
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-12-2009, 03:39 PM
Member
 
Join Date: Oct 2009
Posts: 48
Rep Power: 0
cowboy is on a distinguished road
Default
I hope so on and on
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lazy associations – IV JavaForums Java Blogs 0 08-31-2008 02:30 PM
Lazy associations – II JavaForums Java Blogs 0 08-30-2008 01:40 PM
Lazy associations – I JavaForums Java Blogs 0 08-30-2008 01:40 PM
Lazy Initialization onegcr New To Java 1 08-14-2007 03:29 PM
hibernate lazy=False Ed Database 2 07-02-2007 07:54 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:21 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org