-->
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.  [ 5 posts ] 
Author Message
 Post subject: Is it possible to dynamically change Fetch types?
PostPosted: Sat Mar 10, 2007 4:04 am 
Newbie

Joined: Sat Mar 10, 2007 2:43 am
Posts: 4
Is it possibly to dyamically change Fetch.type? I have an entity that is used in a high transaction application and I want to optionally retrieve associations. ie. Either the associations will be all used or none at all. Lazy loading is not an option because it will result in too many atomic queries being executed.

I guess what I really want is to use is FetchType.EAGER (always creates an efficient single query) with the option to dynamically disable the Fetching of assocations.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 10, 2007 10:56 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
It is obviously possible. This kind of choices are what is called a "fetch plan". What is in your mappings is only the default behaviour.

Maybe you could show the query you tried. I don't know JPA very well as I'm using Hibernate directly. But with the criteria API, forcing a join is just a matter of calling setFetchMode("theRelation", FetchMode.JOIN);.

With HQL, just add "left join theRelation" to your expression, and you're done.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 11, 2007 4:26 pm 
Newbie

Joined: Sun Jan 14, 2007 2:52 pm
Posts: 19
I am trying to use a FetchMode.join in a criteria as you have described. I think I may be misunderstanding what the result should be.

My simple classes are Account and User, where an Account can have many users and and user can have one account.

Account:
Code:
public class Account implements Serializable {
...
@OneToMany(mappedBy="account", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Set<User> users = new HashSet<User>();
...
}


User:
Code:
public class User implements Serializable {
...
@ManyToOne (targetEntity=org.martingilday.webapp.domain.Account.class)
@JoinColumn (name="FK_AccountID", nullable=false)
private Account account;
...
}


My query is as so
Code:
return session.createCriteria(getPersistentClass()).setFetchMode("users", FetchMode.JOIN).list();


I am trying to load all accounts and have each of their user collections constructed using just one sql statement. However when I use FetchMode.JOIN I end up with more results than anticipated. If I have two accounts each with three users then the size of my list will be six. It was my understanding that Hibernate would translate the resultset into 2 account objects with the correct users attached. Is this not the case?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 11, 2007 4:57 pm 
Newbie

Joined: Sun Jan 14, 2007 2:52 pm
Posts: 19
Answering my question : http://forum.hibernate.org/viewtopic.ph ... 13#2343613


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 11, 2007 5:45 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Yes. Indeed.

Try this instead, it should do what you were expecting at first:
Code:
return session.createCriteria(getPersistentClass()).setFetchMode("users", FetchMode.JOIN).setResultTransformer(ICriteria.DISTINCT_ROOT_ENTITY).list();

HTH :-)

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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