-->
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.  [ 4 posts ] 
Author Message
 Post subject: OnoToOne Join Problem
PostPosted: Sat May 06, 2006 5:19 pm 
Newbie

Joined: Sun Mar 06, 2005 3:18 pm
Posts: 7
I cannot seem to get the desired affect when querying for users in my application. Consider that I have an object User that has a property Credentials. I am using separate objects because Credentials may be possessed by other objects than Users. So, I do not want any user specific data in the Credentials object/schema.

I use the following annotations in my User object:

Code:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "cred_pk", referencedColumnName="cred_id")
public Credentials getCredentials() {
   return credentials;
}

Credentials does not have any mapping info other than the requisite @Id and @Column properties.

The behavior occurs when I get the list of users using the following:
Code:
Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from User as user");
List<User> l = q.list();
session.getTransaction().commit();

I see in the hql output that it selects from users once as desired. However, rather than joining to credentials (as I'd suspect) it performs n separate select statements per each user returned. Clearly, this is inefficient.

How can I ensure that a single select is issued to return all of the data I am looking for?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 06, 2006 6:32 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
The fetch strategie is missing.

Best Regards

Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 06, 2006 7:36 pm 
Newbie

Joined: Sun Mar 06, 2005 3:18 pm
Posts: 7
I changed the @OneToOne to

Code:
@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)


It still exhibits the behavior described.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 2:44 am 
Newbie

Joined: Sun Mar 06, 2005 3:18 pm
Posts: 7
So, I've changed my hql to:

Code:
"from User as user inner join user.credentials as credentials"

Now I get the data back in one query, but two objects. I have to loop over the entire result set and cast Object[0] and [1] to User and Credentials, respectively. Then I have to add the creds to my user and place it in a new list. Essentially, I've traded a sql inefficiency for a code inefficiency. I suppose that's better if volume's high, but if the result set is large I'll be in for a dog of a page load/heap allocation. Seems I can't win here.


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