Bael 1908 - Guide to SqlResultSetMapping (#4586)
* Commit for Eval Article pull request * Revert "Commit for Eval Article pull request" * BAEL-1908 Initial Commit
This commit is contained in:
parent
28166a73de
commit
f3237ef2c5
32
persistence-modules/java-jpa/pom.xml
Normal file
32
persistence-modules/java-jpa/pom.xml
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>java-jpa</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-core</artifactId>
|
||||||
|
<version>${hibernate.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>${h2.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<hibernate.version>5.3.1.Final</hibernate.version>
|
||||||
|
<h2.version>1.4.197</h2.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.sqlresultsetmapping;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
|
||||||
|
@SqlResultSetMapping(
|
||||||
|
name="EmployeeResult",
|
||||||
|
entities={
|
||||||
|
@EntityResult(
|
||||||
|
entityClass = com.baeldung.sqlresultsetmapping.Employee.class,
|
||||||
|
fields={@FieldResult(name="id",column="employeeNumber"),
|
||||||
|
@FieldResult(name="name", column="name")}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@NamedNativeQuery(
|
||||||
|
name="Employees",
|
||||||
|
query="SELECT id as employeeNumber, name FROM EMPLOYEE",
|
||||||
|
resultSetMapping = "EmployeeResult"
|
||||||
|
)
|
||||||
|
@Entity
|
||||||
|
public class Employee {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package com.baeldung.sqlresultsetmapping;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@SqlResultSetMappings(value = {
|
||||||
|
@SqlResultSetMapping(name = "ScheduleResult",
|
||||||
|
classes = { @ConstructorResult(targetClass = com.baeldung.sqlresultsetmapping.ScheduledDay.class,
|
||||||
|
columns = { @ColumnResult(name = "id", type = Long.class),
|
||||||
|
@ColumnResult(name = "employeeId", type = Long.class),
|
||||||
|
@ColumnResult(name = "hourIn"),
|
||||||
|
@ColumnResult(name = "hourOut"),
|
||||||
|
@ColumnResult(name = "dayOfWeek") }) }),
|
||||||
|
@SqlResultSetMapping(name = "FridayEmployeeResult",
|
||||||
|
columns = { @ColumnResult(name = "employeeId") }),
|
||||||
|
@SqlResultSetMapping(name = "EmployeeScheduleResults",
|
||||||
|
entities = { @EntityResult(entityClass = com.baeldung.sqlresultsetmapping.Employee.class),
|
||||||
|
@EntityResult(entityClass = com.baeldung.sqlresultsetmapping.ScheduledDay.class)
|
||||||
|
}) })
|
||||||
|
@NamedNativeQuery(name = "FridayEmployees",
|
||||||
|
query = "SELECT employeeId FROM schedule_days WHERE dayOfWeek = 'FRIDAY'",
|
||||||
|
resultSetMapping = "FridayEmployeeResult")
|
||||||
|
|
||||||
|
@NamedNativeQuery(name = "Schedules",
|
||||||
|
query = "SELECT * FROM schedule_days WHERE hourIn = 8",
|
||||||
|
resultSetMapping = "ScheduleResult")
|
||||||
|
@Entity
|
||||||
|
@Table(name = "SCHEDULE_DAYS")
|
||||||
|
public class ScheduledDay {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
private Long employeeId;
|
||||||
|
private Integer hourIn;
|
||||||
|
private Integer hourOut;
|
||||||
|
private String dayOfWeek;
|
||||||
|
|
||||||
|
public ScheduledDay() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledDay(Long id, Long employeeId, Integer hourIn, Integer hourOut, String dayofWeek) {
|
||||||
|
this.id = id;
|
||||||
|
this.employeeId = employeeId;
|
||||||
|
this.hourIn = hourIn;
|
||||||
|
this.hourOut = hourOut;
|
||||||
|
this.dayOfWeek = dayofWeek;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getEmployeeId() {
|
||||||
|
return employeeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmployeeId(Long employeeId) {
|
||||||
|
this.employeeId = employeeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getHourIn() {
|
||||||
|
return hourIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHourIn(Integer hourIn) {
|
||||||
|
this.hourIn = hourIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getHourOut() {
|
||||||
|
return hourOut;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHourOut(Integer hourOut) {
|
||||||
|
this.hourOut = hourOut;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDayOfWeek() {
|
||||||
|
return dayOfWeek;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDayOfWeek(String dayOfWeek) {
|
||||||
|
this.dayOfWeek = dayOfWeek;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||||
|
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
|
||||||
|
version="2.1">
|
||||||
|
|
||||||
|
<persistence-unit name="java-jpa-scheduled-day">
|
||||||
|
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||||
|
<class>com.baeldung.sqlresultsetmapping.ScheduledDay</class>
|
||||||
|
<properties>
|
||||||
|
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
||||||
|
<property name="javax.persistence.jdbc.url"
|
||||||
|
value="jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:database.sql'" />
|
||||||
|
<property name="javax.persistence.jdbc.user" value="sa" />
|
||||||
|
<property name="javax.persistence.jdbc.password" value="" />
|
||||||
|
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
|
||||||
|
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop" />-->
|
||||||
|
<property name="show_sql" value="true"/>
|
||||||
|
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
|
||||||
|
</properties>
|
||||||
|
</persistence-unit>
|
||||||
|
</persistence>
|
19
persistence-modules/java-jpa/src/main/resources/database.sql
Normal file
19
persistence-modules/java-jpa/src/main/resources/database.sql
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
CREATE TABLE EMPLOYEE
|
||||||
|
(id BIGINT,
|
||||||
|
name VARCHAR(10));
|
||||||
|
|
||||||
|
INSERT INTO EMPLOYEE VALUES (1, 'JOHN');
|
||||||
|
INSERT INTO EMPLOYEE VALUES (2, 'MARY');
|
||||||
|
INSERT INTO EMPLOYEE VALUES (3, 'FRANK');
|
||||||
|
|
||||||
|
CREATE TABLE SCHEDULE_DAYS
|
||||||
|
(id IDENTITY,
|
||||||
|
employeeId BIGINT,
|
||||||
|
hourIn int,
|
||||||
|
hourOut int,
|
||||||
|
dayOfWeek VARCHAR(10));
|
||||||
|
|
||||||
|
INSERT INTO SCHEDULE_DAYS (employeeId, hourIn, hourOut, dayOfWeek) VALUES (1, 13, 21, 'FRIDAY');
|
||||||
|
INSERT INTO SCHEDULE_DAYS (employeeId, hourIn, hourOut, dayOfWeek) VALUES (2, 8, 4, 'SATURDAY');
|
||||||
|
INSERT INTO SCHEDULE_DAYS (employeeId, hourIn, hourOut, dayOfWeek) VALUES (3, 8, 4, 'MONDAY');
|
||||||
|
INSERT INTO SCHEDULE_DAYS (employeeId, hourIn, hourOut, dayOfWeek) VALUES (3, 8, 4, 'FRIDAY');
|
@ -0,0 +1,71 @@
|
|||||||
|
package com.baeldung.sqlresultsetmapping;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.persistence.Persistence;
|
||||||
|
import javax.persistence.Query;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SqlResultSetMappingUnitTest {
|
||||||
|
|
||||||
|
private static EntityManager em;
|
||||||
|
private static EntityManagerFactory emFactory;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void setup() {
|
||||||
|
emFactory = Persistence.createEntityManagerFactory("java-jpa-scheduled-day");
|
||||||
|
em = emFactory.createEntityManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNamedQuery_thenColumnResult() {
|
||||||
|
List<Long> employeeIds = em.createNamedQuery("FridayEmployees").getResultList();
|
||||||
|
assertEquals(2, employeeIds.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNamedQuery_thenConstructorResult() {
|
||||||
|
List<ScheduledDay> scheduleDays = Collections.checkedList(em.createNamedQuery("Schedules", ScheduledDay.class).getResultList(), ScheduledDay.class);
|
||||||
|
assertEquals(3, scheduleDays.size());
|
||||||
|
assertTrue(scheduleDays.stream().allMatch(c -> c.getHourIn().longValue() == 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNamedQuery_thenSingleEntityResult() {
|
||||||
|
List<Employee> employees = Collections.checkedList(em.createNamedQuery("Employees").getResultList(), Employee.class);
|
||||||
|
assertEquals(3, employees.size());
|
||||||
|
assertTrue(employees.stream().allMatch(c -> c.getClass() == Employee.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNamedQuery_thenMultipleEntityResult() {
|
||||||
|
final Query query = em.createNativeQuery("SELECT e.id, e.name, d.id, d.employeeId, "
|
||||||
|
+ " d.dayOfWeek, d.hourIn, d.hourOut "
|
||||||
|
+ " FROM employee e, schedule_days d "
|
||||||
|
+ " WHERE e.id = d.employeeId", "EmployeeScheduleResults");
|
||||||
|
List<Object[]> results = query.getResultList();
|
||||||
|
assertEquals(4, results.size());
|
||||||
|
assertTrue(results.get(0).length == 2);
|
||||||
|
|
||||||
|
Employee emp = (Employee) results.get(1)[0];
|
||||||
|
ScheduledDay day = (ScheduledDay) results.get(1)[1];
|
||||||
|
|
||||||
|
assertTrue(day.getEmployeeId() == emp.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void destroy() {
|
||||||
|
|
||||||
|
if (em != null) {
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
if (emFactory != null) {
|
||||||
|
emFactory.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO employee (1, "JOHN");
|
||||||
|
INSERT INTO employee (2, "MARY");
|
||||||
|
INSERT INTO employee (3, "FRANK");
|
@ -0,0 +1,10 @@
|
|||||||
|
INSERT INTO SCHEDULE_DAYS (1, 13, 21, 'FRIDAY');
|
||||||
|
INSERT INTO SCHEDULE_DAYS (2, 8, 4, 'SATURDAY');
|
||||||
|
INSERT INTO SCHEDULE_DAYS (3, 8, 4, 'FRIDAY');
|
||||||
|
|
||||||
|
|
||||||
|
-- private Long id;
|
||||||
|
-- private Long employeeId;
|
||||||
|
-- private Time in;
|
||||||
|
-- private Time out;
|
||||||
|
-- private DayOfWeek dayOfWeek;
|
Loading…
x
Reference in New Issue
Block a user