There are 2 entities:

@Entity @Table(name = "page") public class Page { @Id int id; } 

and

 @Entity @Table(name = "page_details") public class PageDetails { @Id private int id; @OneToOne(fetch = FetchType.EAGER) @JoinColumn(name = "id", referencedColumnName = "id") @Fetch(FetchMode.JOIN) private Page page; } 

When requesting all PageDetails via JpaRepository.findAll, I expect to receive 1 request of the form:

 select * from page inner join page_details on page.id = page_details.id; 

But instead I get 1 request:

 select * from page; 

and N queries:

 select * from page_details where id = ?; 

How can this be fixed? Hibernate version 5.0.9

    1 answer 1

    Hiberneit sometimes ignores EAGER initialization and still uses LAZY. Personally, I deal with this through JQL and FETCH JOIN.

    those. In your case, you need to create a new method in the repository of the form:

     @Queury("SELECT d FROM PageDetails d JOIN FETCH d.page") List<PageDetail> selectAll(); 

    In some situations it is possible to use the same "LEFT OUTER", but it depends on your situation, you can read more about it at here (see the sections below under the list - INNER JOIN, etc.)

    • Do I understand correctly that this is an undocumented behavior of hibernate (or even a bug) or was it really intended? - Victor Khovanskiy
    • As I understand this is a feature of the JPA specification. Here: stackoverflow.com/questions/463349/… you can read a little more. Unfortunately, I do not have a more detailed description of this problem. Maybe one of the participants of the CO can answer better. - Vartlok