I'm using Hibernate 3.3.0.GA and I noticed some strange behavior, not documented (I think).
I noticed that entityManager.find
resolve the EAGER
relationships of my entity and entityManager.createQuery
not.
Per example, if I have an entity:
@Entity
public class Person {
@Id
private int id;
private String name;
@ManyToOne //EAGER by default in JPA
private Address address;
}
The generated SQL with entityManager.find(Person.class, 1L)
:
select
person0_.id as id1_1_,
person0_.address_id as address3_1_1_,
person0_.name as name1_1_,
address1_.id as id2_0_
from
Person person0_
left outer join
Address address1_
on person0_.address_id=address1_.id
where
person0_.id=?
And the generated SQL with entityManager.createQuery("SELECT p FROM Person where id = 1")
:
select
person0_.id as id1_,
person0_.address_id as address3_1_,
person0_.name as name1_
from
Person person0_
where
person0_.id=?
So, is there a explanation about why this happen? For me, both need to have the same behavior.
Working example
I create an example in my repository showing that problem, using Hibernate 4.1.6.Final: https://github.com/dherik/hibernate-find-em-so-question. Just use mvn clean install
and the console will print the queries.
Updated
@KlausGroenbaek said the EclipseLink 2.5.2 have the same behaviour in the two methods. He did a Hibernate example too and obtain the similar result in the two methods (find
it does a join fetch and createQuery
it does multiple selects, both EAGER
by definition).