Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: lazy loading of subClass
PostPosted: Wed Nov 25, 2009 3:06 am 
Beginner
Beginner

Joined: Mon Mar 02, 2009 3:36 pm
Posts: 25
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;
}
}


Code:
public class Disc {
private int id ;
public int getId() {
   return id;
}
public void setId(int id) {
   this.id = id;
}
}


Code:
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;
}
}


Code:
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.


Top
 Profile  
 
 Post subject: Re: lazy loading of subClass
PostPosted: Wed Nov 25, 2009 7:17 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Quote:
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").


With that attribute you can disable the lazy-loading-feature, but you do not say that this (extended) part of the object is loaded eagerly ,which is not possible at all.

For lazy property fetching you still can set even your basic attributes to lazy (which is not recommended).

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: lazy loading of subClass
PostPosted: Wed Nov 25, 2009 7:34 am 
Beginner
Beginner

Joined: Mon Mar 02, 2009 3:36 pm
Posts: 25
This is mapping file :

Code:
<hibernate-mapping>
   <class name="tablepersubclass.Disc" polymorphism="explicit" lazy="true"  table="DB2ADMIN.DISC_PERSUBCLASS">
      <id name="id" type="integer" column="ID" >
         <generator class ="assigned"/>
      </id>
      <joined-subclass name="tablepersubclass.AudioDisc"  lazy="true"  extends="tablepersubclass.Disc" table="DB2ADMIN.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="tablepersubclass.VideoDisc" lazy="true" extends="tablepersubclass.Disc" table="DB2ADMIN.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>


when i am calling reservation.getDiscs() the following sql is fired :

Code:
select discs0_.DISC_ID as DISC2_1_, discs0_.ID as ID1_, discs0_.ID as ID2_0_, discs0_1_.SINGER as SINGER1_0_, discs0_1_.NO_OF_SONGS as NO3_1_0_, discs0_2_.DIRECTOR as DIRECTOR4_0_, discs0_2_.LANG as LANG4_0_, case when discs0_1_.DISC_ID is not null then 1 when discs0_2_.DISC_ID is not null then 2 when discs0_.ID is not null then 0 end as clazz_0_ from DB2ADMIN.DISC_PERSUBCLASS discs0_ left outer join DB2ADMIN.AUDIODISC_PERSUBCLASS discs0_1_ on discs0_.ID=discs0_1_.DISC_ID left outer join DB2ADMIN.VIDEODISC_PERSUBCLASS discs0_2_ on discs0_.ID=discs0_2_.DISC_ID where discs0_.DISC_ID=?


on a particular use-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.

Thanks.


Top
 Profile  
 
 Post subject: Re: lazy loading of subClass
PostPosted: Wed Nov 25, 2009 7:37 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, you know it, but hibernate does not and can not know this. You should change your mapping if you want to tell hibernate to load only audio-discs.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: lazy loading of subClass
PostPosted: Wed Nov 25, 2009 8:03 am 
Beginner
Beginner

Joined: Mon Mar 02, 2009 3:36 pm
Posts: 25
what changes i will have to make ?


Top
 Profile  
 
 Post subject: Re: lazy loading of subClass
PostPosted: Wed Nov 25, 2009 8:18 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, I dont know your special use-case, but you could either map another collection which is contains only audiodiscs. Another thing you can try is use a HQL-Query to load the objects and restrict on the class, e.g.:
Code:
from Cat cat where cat.class = DomesticCat

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: lazy loading of subClass
PostPosted: Wed Nov 25, 2009 8:21 am 
Beginner
Beginner

Joined: Mon Mar 02, 2009 3:36 pm
Posts: 25
Ok . Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 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.