-->
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.  [ 9 posts ] 
Author Message
 Post subject: lazy loading
PostPosted: Thu Feb 08, 2007 8:21 am 
Newbie

Joined: Sun Apr 16, 2006 1:02 pm
Posts: 12
Hi!

I don't quite understand Hibernate behaviour of loading entities. Two simple Tables: html and text (extracted text from html):

Code:
public class Html
{
    @OneToOne( fetch = FetchType.LAZY, cascade = CascadeType.ALL )
    @PrimaryKeyJoinColumn
    private Text text;
   


Now I want to select all HTML records that haven't been parsed yet:

Code:

FROM Html h WHERE h.urlId != ALL (SELECT urlId FROM Text)


This yields to a single SQL statement that does what it's supposed to do but also each single Text row to be loaded. If I remove the Text relation from Html everything's fine.


Top
 Profile  
 
 Post subject: lazy loading
PostPosted: Thu Feb 08, 2007 11:23 am 
Beginner
Beginner

Joined: Wed Jan 03, 2007 7:47 pm
Posts: 23
Location: Richardson, Texas
Hello,

I was just working with a similar one-to-one relationship which shares the same PK. I experienced the same behavior as you mentioned regardless of the FetchType setting.

I don't know the reason behind this behavior but I was experimenting with the hibernate extension

Code:
@org.hibernate.annotations.Fetch(
    org.hibernate.annotations.FetchMode.JOIN
)


The data from the secondary row will still load but hibernate will execute a single joined select.

Regards,
Kurt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 11:50 am 
Newbie

Joined: Sun Apr 16, 2006 1:02 pm
Posts: 12
Hmm, nope, unfortunately not
Code:
public class GzHtml
{
   @Id
   @Column( name = "url_id" )
   private String urlId;

   @Column( name = "html" )
   @NotNull
   private byte[] html;

   @OneToOne( fetch = FetchType.LAZY, cascade = CascadeType.ALL )
   @PrimaryKeyJoinColumn
   @org.hibernate.annotations.Fetch( org.hibernate.annotations.FetchMode.JOIN )   
   private GzText gzText;

Code:
Hibernate:
    /* load f4t.model.GzText */ select
        gztext0_.url_id as url1_2_0_,
        gztext0_.title as title2_0_,
        gztext0_.summary as summary2_0_,
        gztext0_.text as text2_0_
    from
        public.gztext gztext0_
    where
        gztext0_.url_id=?
Hibernate:
    /* load f4t.model.GzText */ select
        gztext0_.url_id as url1_2_0_,
        gztext0_.title as title2_0_,
        gztext0_.summary as summary2_0_,
        gztext0_.text as text2_0_
    from
        public.gztext gztext0_
    where
...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 11:52 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If you type "lazy one-to-one" into the search field on the Hibernate website, you get this: http://www.hibernate.org/162.html

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 12:05 pm 
Beginner
Beginner

Joined: Wed Jan 03, 2007 7:47 pm
Posts: 23
Location: Richardson, Texas
Christian,

That link provides a good explanation, thanks for the response.

Regards,
Kurt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 12:32 pm 
Newbie

Joined: Sun Apr 16, 2006 1:02 pm
Posts: 12
Yes (I even did read the page but apparently not thoroughly). Unfortunately it doesn't solve the problem :\

So, this means another use-case specific domain model might be required, e.g. if the use-case requires to quickly read a vast amount of contrained data without all possible (but use-case useless) relations resolved. E.g. when extracting content from an corporate CMS in order to build a search index. Or some @hack annotations :)

BTW is there some page helping to map .hbm.xml @annotations?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Or it could be that...

- Your FetchMode.JOIN didn't work because you also enabled FetchType.LAZY. Use FetchType.EAGER + FetchMode.JOIN if you want eager join fetching, or FetchType.EAGER + FetchMode.SELECT if you want eager fetching with a secondary select. This is also explained in the reference documentation(s) and there is even another extra page on the Wiki about it for people who don't like documentation. It's called "A primer on fetching strategies".

- If you still want FetchType.LAZY, and proxies don't work for you (and shared PK one-to-zero-or-one is such a rare exception), use @LazyToOne and run the bytecode enhancer to inject interception code instead. You can find this in the reference documentation as well.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 2:41 pm 
Newbie

Joined: Sun Apr 16, 2006 1:02 pm
Posts: 12
christian wrote:
- Your FetchMode.JOIN didn't work because you also enabled FetchType.LAZY. Use FetchType.EAGER + FetchMode.JOIN if you want eager join fetching, or FetchType.EAGER + FetchMode.SELECT if you want eager fetching with a secondary select.


Maybe I got this wrong but this still loads the Text entities with a bunch of single selects.

christian wrote:
- If you still want FetchType.LAZY, and proxies don't work for you (and shared PK one-to-zero-or-one is such a rare exception), use @LazyToOne and run the bytecode enhancer to inject interception code instead. You can find this in the reference documentation as well.


I remember having read @LazyToOne but the bytecode enhancer detered me. But certainly a better solution than @hack annotations :) I'm going to try this tomorrow.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 10, 2007 9:38 am 
Newbie

Joined: Sun Apr 16, 2006 1:02 pm
Posts: 12
Hm. I'd a shot an @LazyToOne but it still loads all these Text entities. Yes, I ran the cglib enhancer and I even decompiled the class:
Code:
    public GzText getGzText()
    {
        return $cglib_read_gzText();
    }

Code:
   // http://www.hibernate.org/162.html
   @OneToOne( fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = true )
   @LazyToOne( value = LazyToOneOption.PROXY )
   @PrimaryKeyJoinColumn
   private GzText gzText;


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