Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA: Query that returns multiple entities

I'm writing a JPQL query that joins across three tables. In my resultlist I would like to get all three entities per matching row (hope that makes sense).

Any ideas?

Hibernate 3.x is my JPA provider.

like image 863
Justin Kredible Avatar asked Jul 29 '11 19:07

Justin Kredible


People also ask

How do I return multiple entities in JPA query?

3.1. In order to create a query returning multiple different entities, we need to do 2 things. Firstly, we need to list entities that we want to return in the SELECT part of the SQL Query, separated by a comma. Secondly, we need to connect them with each other by their primary and corresponding foreign keys.

How do I save multiple entities in JPA?

Basically what you are looking for is batch insert into database using JPA. These topics have already been brought up, these will help you: JPA/Hibernate bulk(batch) insert. Batch inserts with JPA/EJB3.

How do I join two entities in JPA?

The only way to join two unrelated entities with JPA 2.1 and Hibernate versions older than 5.1, is to create a cross join and reduce the cartesian product in the WHERE statement. This is harder to read and does not support outer joins. Hibernate 5.1 introduced explicit joins on unrelated entities.

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.


1 Answers

IIRC, you can do a SELECT o1, o2, o3 FROM EntityA o1, EntityB o2, EntityC o3 WHERE ...., and the result will be a List<Object[3]>, where the array contents will contain the o1,o2,o3 values.

like image 120
Tassos Bassoukos Avatar answered Sep 28 '22 08:09

Tassos Bassoukos