When the SELECT statement only selects some attributes of the query persistence class, the query result returned by Hibernate is relational data rather than a persistent object. For example:
From customer C inner join C. Orders O group by C. Age
Select C. ID, C. Name, C. Age, O. ID, O. order_number, O. customer_id from customer C inner join C. Orders O group by C. Age
The preceding two hql query statements correspond to the same SQL statement, so the same data in the database can be queried. The difference is that the former returns
The Order persistence objects are stored in the session cache, and the session will ensure their uniqueness. The latter returns relational data, which does not occupy session cache,
As long as there are no variables in the application to apply the data, the memory they occupy can be recycled by the JVM garbage collector.
Therefore, the second method can improve the query performance. As long as the application no longer references the data, the memory they occupy will be released.
For the second form, you can define a Javabean to overpack the relational data in the query results, so that the application can still access the query results in an object-oriented way. For example:
Select from new customerorder (C. ID, C. Name, C. Age, O. ID, O. order_number, O. customer_id)
From customer C inner join C. Orders O group by C. Age
The customerorder class is not a persistence class, and its instance will not be added to the session cache.