BAEL-4887: fetching multiple entities in JPA query (#10803)
This commit is contained in:
parent
c24379b927
commit
27c53fd6de
@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.jpa.returnmultipleentities;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Channel {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
private Long subscriptionId;
|
||||||
|
|
||||||
|
public void setCode(String code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriptionId(Long subscriptionId) {
|
||||||
|
this.subscriptionId = subscriptionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Channel channel = (Channel) o;
|
||||||
|
return Objects.equals(id, channel.id) && Objects.equals(code, channel.code) && Objects.equals(subscriptionId, channel.subscriptionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, code, subscriptionId);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.jpa.returnmultipleentities;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.persistence.Persistence;
|
||||||
|
import javax.persistence.Query;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ReportRepository {
|
||||||
|
private final EntityManagerFactory emf;
|
||||||
|
|
||||||
|
public ReportRepository() {
|
||||||
|
emf = Persistence.createEntityManagerFactory("jpa-h2-return-multiple-entities");
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Object[]> find(String email) {
|
||||||
|
EntityManager entityManager = emf.createEntityManager();
|
||||||
|
Query query = entityManager.createQuery("SELECT c, s, u FROM Channel c, Subscription s, User u WHERE c.subscriptionId = s.id AND s.id = u.subscriptionId AND u.email=:email");
|
||||||
|
query.setParameter("email", email);
|
||||||
|
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.baeldung.jpa.returnmultipleentities;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Subscription {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
public void setCode(String code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Subscription subscription = (Subscription) o;
|
||||||
|
return Objects.equals(id, subscription.id) && Objects.equals(code, subscription.code);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, code);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.jpa.returnmultipleentities;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class User {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
private Long subscriptionId;
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriptionId(Long subscriptionId) {
|
||||||
|
this.subscriptionId = subscriptionId;
|
||||||
|
}
|
||||||
|
}
|
@ -113,4 +113,24 @@
|
|||||||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
|
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
|
||||||
</properties>
|
</properties>
|
||||||
</persistence-unit>
|
</persistence-unit>
|
||||||
|
<persistence-unit name="jpa-h2-return-multiple-entities">
|
||||||
|
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||||
|
<class>com.baeldung.jpa.returnmultipleentities.Channel</class>
|
||||||
|
<class>com.baeldung.jpa.returnmultipleentities.Subscription</class>
|
||||||
|
<class>com.baeldung.jpa.returnmultipleentities.User</class>
|
||||||
|
<class>com.baeldung.jpa.returnmultipleentities.ReportRepository</class>
|
||||||
|
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||||
|
<properties>
|
||||||
|
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
|
||||||
|
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
|
||||||
|
<property name="javax.persistence.jdbc.user" value="sa"/>
|
||||||
|
<property name="javax.persistence.jdbc.password" value=""/>
|
||||||
|
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
|
||||||
|
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
|
||||||
|
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
|
||||||
|
<property name="hibernate.show_sql" value="true"/>
|
||||||
|
<property name="hibernate.format_sql" value="true"/>
|
||||||
|
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
|
||||||
|
</properties>
|
||||||
|
</persistence-unit>
|
||||||
</persistence>
|
</persistence>
|
||||||
|
@ -0,0 +1,84 @@
|
|||||||
|
package com.baeldung.jpa.returnmultipleentities;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.persistence.Persistence;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
public class ReturnMultipleEntitiesIntegrationTest {
|
||||||
|
private static EntityManagerFactory factory;
|
||||||
|
private static EntityManager entityManager;
|
||||||
|
private ReportRepository reportRepository;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
factory = Persistence.createEntityManagerFactory("jpa-h2-return-multiple-entities");
|
||||||
|
entityManager = factory.createEntityManager();
|
||||||
|
reportRepository = new ReportRepository();
|
||||||
|
populateH2DB();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenQueryingForMultipleEntitiesInOneQuery_thenJPAReturnsMultipleEntitiesInCorrectOrder() {
|
||||||
|
List<Object[]> reportDetails = reportRepository.find("user1@gmail.com");
|
||||||
|
|
||||||
|
assertEquals(2, reportDetails.size());
|
||||||
|
|
||||||
|
for (Object[] reportDetail : reportDetails) {
|
||||||
|
assertEquals(3, reportDetail.length);
|
||||||
|
|
||||||
|
Channel channel = (Channel) reportDetail[0];
|
||||||
|
Subscription subscription = (Subscription) reportDetail[1];
|
||||||
|
User user = (User) reportDetail[2];
|
||||||
|
|
||||||
|
assertEquals("single", subscription.getCode());
|
||||||
|
assertEquals("user1@gmail.com", user.getEmail());
|
||||||
|
if (!("eurosport".equals(channel.getCode()) || "hbo".equals(channel.getCode()))) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void populateH2DB() {
|
||||||
|
entityManager.getTransaction().begin();
|
||||||
|
|
||||||
|
Subscription single = new Subscription();
|
||||||
|
single.setCode("single");
|
||||||
|
|
||||||
|
Subscription family = new Subscription();
|
||||||
|
family.setCode("family");
|
||||||
|
|
||||||
|
entityManager.persist(single);
|
||||||
|
entityManager.persist(family);
|
||||||
|
|
||||||
|
Channel bbc = new Channel();
|
||||||
|
bbc.setCode("bbc");
|
||||||
|
bbc.setSubscriptionId(family.getId());
|
||||||
|
|
||||||
|
Channel eurosport = new Channel();
|
||||||
|
eurosport.setCode("eurosport");
|
||||||
|
eurosport.setSubscriptionId(single.getId());
|
||||||
|
|
||||||
|
Channel hbo = new Channel();
|
||||||
|
hbo.setCode("hbo");
|
||||||
|
hbo.setSubscriptionId(single.getId());
|
||||||
|
|
||||||
|
entityManager.persist(bbc);
|
||||||
|
entityManager.persist(eurosport);
|
||||||
|
entityManager.persist(hbo);
|
||||||
|
|
||||||
|
User user1 = new User();
|
||||||
|
user1.setEmail("user1@gmail.com");
|
||||||
|
user1.setSubscriptionId(single.getId());
|
||||||
|
|
||||||
|
entityManager.persist(user1);
|
||||||
|
|
||||||
|
entityManager.getTransaction().commit();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user