BAEL-1908 Editor Feedback - simplification of Entity
This commit is contained in:
parent
b6ca7d58b2
commit
2c7729c7d3
|
@ -7,8 +7,6 @@ import javax.persistence.*;
|
||||||
classes = { @ConstructorResult(targetClass = com.baeldung.sqlresultsetmapping.ScheduledDay.class,
|
classes = { @ConstructorResult(targetClass = com.baeldung.sqlresultsetmapping.ScheduledDay.class,
|
||||||
columns = { @ColumnResult(name = "id", type = Long.class),
|
columns = { @ColumnResult(name = "id", type = Long.class),
|
||||||
@ColumnResult(name = "employeeId", type = Long.class),
|
@ColumnResult(name = "employeeId", type = Long.class),
|
||||||
@ColumnResult(name = "hourIn"),
|
|
||||||
@ColumnResult(name = "hourOut"),
|
|
||||||
@ColumnResult(name = "dayOfWeek") }) }),
|
@ColumnResult(name = "dayOfWeek") }) }),
|
||||||
@SqlResultSetMapping(name = "FridayEmployeeResult",
|
@SqlResultSetMapping(name = "FridayEmployeeResult",
|
||||||
columns = { @ColumnResult(name = "employeeId") }),
|
columns = { @ColumnResult(name = "employeeId") }),
|
||||||
|
@ -21,7 +19,7 @@ import javax.persistence.*;
|
||||||
resultSetMapping = "FridayEmployeeResult")
|
resultSetMapping = "FridayEmployeeResult")
|
||||||
|
|
||||||
@NamedNativeQuery(name = "Schedules",
|
@NamedNativeQuery(name = "Schedules",
|
||||||
query = "SELECT * FROM schedule_days WHERE hourIn = 8",
|
query = "SELECT * FROM schedule_days WHERE employeeId = 3",
|
||||||
resultSetMapping = "ScheduleResult")
|
resultSetMapping = "ScheduleResult")
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "SCHEDULE_DAYS")
|
@Table(name = "SCHEDULE_DAYS")
|
||||||
|
@ -31,18 +29,14 @@ public class ScheduledDay {
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Long id;
|
||||||
private Long employeeId;
|
private Long employeeId;
|
||||||
private Integer hourIn;
|
|
||||||
private Integer hourOut;
|
|
||||||
private String dayOfWeek;
|
private String dayOfWeek;
|
||||||
|
|
||||||
public ScheduledDay() {
|
public ScheduledDay() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduledDay(Long id, Long employeeId, Integer hourIn, Integer hourOut, String dayofWeek) {
|
public ScheduledDay(Long id, Long employeeId, String dayofWeek) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.employeeId = employeeId;
|
this.employeeId = employeeId;
|
||||||
this.hourIn = hourIn;
|
|
||||||
this.hourOut = hourOut;
|
|
||||||
this.dayOfWeek = dayofWeek;
|
this.dayOfWeek = dayofWeek;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,22 +48,6 @@ public class ScheduledDay {
|
||||||
this.employeeId = 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() {
|
public String getDayOfWeek() {
|
||||||
return dayOfWeek;
|
return dayOfWeek;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,9 @@ INSERT INTO EMPLOYEE VALUES (3, 'FRANK');
|
||||||
CREATE TABLE SCHEDULE_DAYS
|
CREATE TABLE SCHEDULE_DAYS
|
||||||
(id IDENTITY,
|
(id IDENTITY,
|
||||||
employeeId BIGINT,
|
employeeId BIGINT,
|
||||||
hourIn int,
|
|
||||||
hourOut int,
|
|
||||||
dayOfWeek VARCHAR(10));
|
dayOfWeek VARCHAR(10));
|
||||||
|
|
||||||
INSERT INTO SCHEDULE_DAYS (employeeId, hourIn, hourOut, dayOfWeek) VALUES (1, 13, 21, 'FRIDAY');
|
INSERT INTO SCHEDULE_DAYS (employeeId, dayOfWeek) VALUES (1, 'FRIDAY');
|
||||||
INSERT INTO SCHEDULE_DAYS (employeeId, hourIn, hourOut, dayOfWeek) VALUES (2, 8, 4, 'SATURDAY');
|
INSERT INTO SCHEDULE_DAYS (employeeId, dayOfWeek) VALUES (2, 'SATURDAY');
|
||||||
INSERT INTO SCHEDULE_DAYS (employeeId, hourIn, hourOut, dayOfWeek) VALUES (3, 8, 4, 'MONDAY');
|
INSERT INTO SCHEDULE_DAYS (employeeId, dayOfWeek) VALUES (3, 'MONDAY');
|
||||||
INSERT INTO SCHEDULE_DAYS (employeeId, hourIn, hourOut, dayOfWeek) VALUES (3, 8, 4, 'FRIDAY');
|
INSERT INTO SCHEDULE_DAYS (employeeId, dayOfWeek) VALUES (3, 'FRIDAY');
|
|
@ -31,8 +31,8 @@ public class SqlResultSetMappingUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenNamedQuery_thenConstructorResult() {
|
public void whenNamedQuery_thenConstructorResult() {
|
||||||
List<ScheduledDay> scheduleDays = Collections.checkedList(em.createNamedQuery("Schedules", ScheduledDay.class).getResultList(), ScheduledDay.class);
|
List<ScheduledDay> scheduleDays = Collections.checkedList(em.createNamedQuery("Schedules", ScheduledDay.class).getResultList(), ScheduledDay.class);
|
||||||
assertEquals(3, scheduleDays.size());
|
assertEquals(2, scheduleDays.size());
|
||||||
assertTrue(scheduleDays.stream().allMatch(c -> c.getHourIn().longValue() == 8));
|
assertTrue(scheduleDays.stream().allMatch(c -> c.getEmployeeId().longValue() == 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -44,8 +44,7 @@ public class SqlResultSetMappingUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenNamedQuery_thenMultipleEntityResult() {
|
public void whenNamedQuery_thenMultipleEntityResult() {
|
||||||
final Query query = em.createNativeQuery("SELECT e.id, e.name, d.id, d.employeeId, "
|
final Query query = em.createNativeQuery("SELECT e.id, e.name, d.id, d.employeeId, d.dayOfWeek "
|
||||||
+ " d.dayOfWeek, d.hourIn, d.hourOut "
|
|
||||||
+ " FROM employee e, schedule_days d "
|
+ " FROM employee e, schedule_days d "
|
||||||
+ " WHERE e.id = d.employeeId", "EmployeeScheduleResults");
|
+ " WHERE e.id = d.employeeId", "EmployeeScheduleResults");
|
||||||
List<Object[]> results = query.getResultList();
|
List<Object[]> results = query.getResultList();
|
||||||
|
|
Loading…
Reference in New Issue