Hibernate : aggregate two entities in one class to retrieve data with one HQL query -
i have 1 class :
public class bookauthor { private book book; private author author; } book , author defined entities.
when try retrieve data, way :
final string selectcols = "new bookauthor(b,a)"; final org.hibernate.query q = createquery(selectcols, params, filters); result = q.list(); return result; when hibernate executes "q.list()", performs 1 query retrieve book's , author's ids, , iterates results information authors , books. is, in log, there :
select a.id, b.id authors , books b and each row :
select a.id, a.name a.id = ? select b.id, b.title b.id = ? is there way tell hibernate results 1 query improve performance ? :
select a.id, a.name, b.id, b.title authors , books b thanks in advance.
edit :
entities defined referring tables , column names like:
public static final string table_name = "author"; @column(name = "id", nullable = false) protected long id; i think there no db constraint between author , book (this example of actual entities, let's there anonymous books no author). jb nizet, retrieve query , post here in while.
you should not @ hibernate queries sql queries. guess after hibernates lazy-loading:
if fetch bookauthor , properties book , author lazy loaded slow (because hibernate has make 3 querys in total 1 entity)
so depending on situation, can disable lazy loading bookauthor in mapping - hibernate fetch data in 1 select query.
Comments
Post a Comment