Various SQL using find and createQuery from Hibernate Entity Manager

I am using Hibernate 3.3.0.GA, and I noticed some strange behavior, not documented (I think).

I noticed that entityManager.findresolving the relationship of EAGERmy essence and entityManager.createQuerynot.

For example, if I have an object:

@Entity
public class Person {

     @Id
     private int id;

     private String name;

     @ManyToOne //EAGER by default in JPA
     private Address address;

}

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 any explanation why this is happening? For me, both should have the same behavior.

Working example

I am creating an example in my repository showing this problem using Hibernate 4.1.6.Final: https://github.com/dherik/hibernate-find-em-so-question . Just use mvn clean installit and the console will print requests.

Update

@KlausGroenbaek , EclipseLink 2.5.2 . Hibernate (find createQuery , EAGER ).

+1
1

, HiberNate 5.2.5 find() , createQuery .

, , , , , , - , .

, , . , , find(), . JPA Java , Persistence . em.clear() (EntityManager), , .

, , EntityManager , , , . EntityManager (Spring JavaEE), , @PersistenceContext, , EntityManager -, ThreadLocal.

JPA 2009 , , , , EntityManager, , Entity EntityManager. , , ( EclipseLink); , , , , Google, , . , JPA, dept, .

+2

Source: https://habr.com/ru/post/1695457/


All Articles