How to build an HQL query that connects subtitles marked with LAZY automatically?

I have an entity:

  public class Album extends GenericAuditedEntity {

    @OneToMany(fetch = FetchType.LAZY)
    private Set<Item> itemSet = new HashSet<Item>();
  }  

And when I start HQL as follows: em.createQuery ("select a from album a"). getResults ()

it processes many SQL queries: One for selecting data from the album table. Smth like this: select .... from Album_table; And one query for each row selected to select items. Smth like this: select .... from Item_table iwhere i.Album_id =: Album_id;

But when I run em.createQuery ("select a.id, b.id from the album a left join Item i") .getResults ()

It processes one SQL query. But this is the result of a list of some parameters that I need to put into objects manually.

HQL ? ?

+3
3

join fetch:

em.createQuery("select a.id, b.id from Album a left join fetch Item i ").getResults();

, , .

+3

join fetch, , Entity, Hibernate

em.createQuery("select a from Album a left join fetch a.itemSet").getResultList();

, , /,

em.createQuery("select new com.xxx.AlbumItem(a.id, b.id) from Album a left join fetch a.itemSet b").getResultList();
+2

.

-1

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


All Articles