Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: How to get result from query with multiple classes

If my query contains one class, like:

query = session.createQuery("select u from User as u"); queryResult = query.list(); 

then I iterate it, where queryResult is an object of User class.

So how to get result from query which contains more than one class? For example:

select u, g from User as u, Group as g where u.groupId = g.groupId and g.groupId = 1 
like image 577
yaya Avatar asked Oct 27 '11 07:10

yaya


People also ask

How can I get result set in hibernate?

Hibernate ResultSet traversing options getResultList() method call. Hibernate also supports scrollable ResultSet cursors through its specific Query. scroll() API. The only apparent advantage of scrollable ResultSets is that we can avoid memory issues on the client-side, since data is being fetched on demand.

Which method is used to get a result from HQL query?

HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.

How does hibernate fetch multiple entities by id?

Load multiple entities by their primary key class ); List<PersonEntity> persons = multiLoadAccess. multiLoad(1L, 2L, 3L); You just need to call the byMultipleIds(Class entityClass) method on the Hibernate Session and provide the class of the entities you want to load as a parameter.

How use native SQL query in hibernate?

For Hibernate Native SQL Query, we use Session. createSQLQuery(String query) to create the SQLQuery object and execute it. For example, if you want to read all the records from Employee table, we can do it through below code. When we execute above code for the data setup we have, it produces following output.


1 Answers

for (Object[] result : query.list()) {     User user = (User) result[0];     Group group = (Group) result[1]; } 
like image 153
PonomarevMM Avatar answered Sep 27 '22 18:09

PonomarevMM