Results 1 to 11 of 11
  1. #1
    ganesh.guddu is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Did you mean "should not be fired untill we have accessed the property of the class"?
    Are you iterating within a session?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    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.

  4. #4
    ganesh.guddu is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote 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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    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.

  6. #6
    ganesh.guddu is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    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.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

  9. #9
    ganesh.guddu is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    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.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    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.

  11. #11
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

Similar Threads

  1. Lazy Initialization
    By onegcr in forum New To Java
    Replies: 1
    Last Post: 08-14-2007, 03:29 PM
  2. hibernate lazy=False
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 07:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •