Hibernate creation does not include fields from joined tables in select clause

I am having a performance issue in my SQL using Hibernate DetachedCriteria. I have several one-two-one relationships, and when Hibernate generates SQL, it includes all the fields from the tables that are combined in FROM. When this happens, it takes MySQL a long time to run the query (which also has order and an additional query, adding to the problem). For my 50K records - 6 sec. When I delete unnecessary fields in SELECT only for the domain object I'm worried about, it works for up to 500 ms.

Is there a way by which Hibernate can not include fields from joins?

I tried setting the fetch parameter in the mapping files to "join" and "select", and it has no meaning in the generated SQL.

I also tried to set a separate root entry, but from what I read, this does not work with paging (which I also do).

I could try and write the query as HQL, but with a subquery it just increases the headache.

+4
source share
3 answers

You can set up a projection containing a list of only those properties that interest you.

Here is an example from a previous project:

Criteria criteria = getSession().createCriteria(Something.class); criteria.createCriteria("user", "u"); // only retrieve the following fields: id, state, viewCount, user.username ProjectionList properties = Projections.projectionList(); properties.add(Projections.property("id")); properties.add(Projections.property("state")); properties.add(Projections.property("viewCount")); properties.add(Projections.property("u.username")); criteria.setProjection(properties); 
+5
source

I have the same question, it seems strange that there is no way to do this. This sounds like a pretty common scenario, use JOIN to filter, but without loading all the associated object (s).

+1
source

Have you tried setting Lazy = true for classes you don't want to load?

0
source

All Articles