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