-->
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.  [ 2 posts ] 
Author Message
 Post subject: Understanding fetch with JPQL
PostPosted: Tue Oct 05, 2010 2:59 pm 
Newbie

Joined: Tue Nov 03, 2009 2:54 pm
Posts: 4
I'm curious about how Hibernate handles the FetchType.

I have the following domain model:
Code:
@Entity
public class T {
  ...
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "t", cascade = { CascadeType.REMOVE, CascadeType.REFRESH })
  private List<TDL> tdls;
  ...
}

@Entity
public class TDL {
  ...
  @ManyToOne(fetch = FetchType.LAZY)
  private T t;

  @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  private ITDL itdl;
  ...
}

@Entity
public class ITDL {
  ...
  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "itdl")
  private DSL dsl;

  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="itdl")
  private TETD ittdl;
  ...
}

@Entity
public class DSL {
  ...
}

@Entity
public class TETD {
  ...
}


When I write a JPQL query such as:
Code:
   select t from T t where t.id = 1


The result set contains "t" with eagerly loaded "tdls". Each "tdl" contains an eagerly loaded "itdl".

Now, because I specified that the ITDL object should lazily load "dsl" and "ittdl" I would expect that each "itdl" would not have "dsl" and "ittdl" loaded. However, I am finding that "dsl" and "ittdl" were actually loaded from this simple jpql query.

I know that @OneToOne and @ManyToOne default to Eager loading while @OneToMany and @ManyToMany default to Lazy loading. In this example I overrode the default @OneToMany on the T object, and the @OneToOne on the ITDL object.

Are the fetch modes in the JPQL query derived by the annotations specified on the entities or do they use the default modes? Can anyone explain why the "dsl" and "ittdl" are NOT lazily loaded?

And, my biggest question is how can I lazily load, from within jpql, @OneToOne relationships. Normally I wouldn't want to do this but because of the complex relationships defined in this entity model and the dataset, the eager fetching of the DSL and ITTDL objects are too expensive. In this case, I don't need these objects loaded and would prefer to lazily load them.


Top
 Profile  
 
 Post subject: Re: Understanding fetch with JPQL
PostPosted: Tue Oct 05, 2010 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi axiopisty,
sorry this is the wrong forum, use the forum for Hibernate core

manyToOne and OneToOne can't lazily load objects unless you can make sure the value can't be null: use the optional property set to false.
Be aware that lazily loading an object means that a proxy is returned instead, so It would be quite hard for a proxy to "become" null later on.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.