BAEL-1908 Initial Commit
This commit is contained in:
+71
@@ -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;
|
||||
Reference in New Issue
Block a user