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:
committed by
Grzegorz Piwowarek
parent
28166a73de
commit
f3237ef2c5
+43
@@ -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;
|
||||
}
|
||||
}
|
||||
+80
@@ -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>
|
||||
@@ -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');
|
||||
Reference in New Issue
Block a user