2018-09-04 00:01:25 +01:00
|
|
|
package com.baeldung.hibernate;
|
|
|
|
|
|
2018-09-05 17:27:25 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2018-09-04 00:01:25 +01:00
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2018-09-11 10:46:05 +01:00
|
|
|
import com.baeldung.hibernate.entities.DeptEmployee;
|
2018-09-04 00:01:25 +01:00
|
|
|
import org.hibernate.Session;
|
|
|
|
|
import org.hibernate.Transaction;
|
|
|
|
|
import org.hibernate.query.Query;
|
2018-09-05 17:27:25 +01:00
|
|
|
import org.hibernate.transform.Transformers;
|
2018-09-05 00:13:08 +01:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
2018-09-04 00:01:25 +01:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
2018-09-05 17:27:25 +01:00
|
|
|
import com.baeldung.hibernate.entities.Department;
|
2018-09-05 00:13:08 +01:00
|
|
|
import com.baeldung.hibernate.pojo.Result;
|
|
|
|
|
|
2018-09-11 10:46:05 +01:00
|
|
|
public class CustomClassIntegrationTest {
|
2018-09-04 00:01:25 +01:00
|
|
|
|
|
|
|
|
private Session session;
|
|
|
|
|
|
|
|
|
|
private Transaction transaction;
|
|
|
|
|
|
2018-09-05 00:13:08 +01:00
|
|
|
@BeforeEach
|
2018-09-04 00:01:25 +01:00
|
|
|
public void setUp() throws IOException {
|
|
|
|
|
session = HibernateUtil.getSessionFactory().openSession();
|
|
|
|
|
transaction = session.beginTransaction();
|
2018-09-05 17:27:25 +01:00
|
|
|
session.createNativeQuery("delete from manager").executeUpdate();
|
|
|
|
|
session.createNativeQuery("delete from department").executeUpdate();
|
|
|
|
|
Department department = new Department("Sales");
|
2018-09-11 10:46:05 +01:00
|
|
|
DeptEmployee employee = new DeptEmployee("John Smith", "001", department);
|
2018-09-05 17:27:25 +01:00
|
|
|
session.persist(department);
|
|
|
|
|
session.persist(employee);
|
2018-09-04 00:01:25 +01:00
|
|
|
transaction.commit();
|
2018-09-05 00:13:08 +01:00
|
|
|
transaction = session.beginTransaction();
|
2018-09-04 00:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-09-05 17:27:25 +01:00
|
|
|
public void whenAllManagersAreSelected_ThenObjectGraphIsReturned() {
|
2018-09-11 10:46:05 +01:00
|
|
|
Query<DeptEmployee> query = session.createQuery("from com.baeldung.hibernate.entities.DeptEmployee");
|
|
|
|
|
List<DeptEmployee> deptEmployees = query.list();
|
|
|
|
|
DeptEmployee deptEmployee = deptEmployees.get(0);
|
|
|
|
|
assertEquals("John Smith", deptEmployee.getName());
|
|
|
|
|
assertEquals("Sales", deptEmployee.getDepartment().getName());
|
2018-09-05 17:27:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenIndividualPropertiesAreSelected_ThenObjectArrayIsReturned() {
|
2018-09-11 10:46:05 +01:00
|
|
|
Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.DeptEmployee m");
|
2018-09-05 17:27:25 +01:00
|
|
|
List managers = query.list();
|
|
|
|
|
Object[] manager = (Object[]) managers.get(0);
|
|
|
|
|
assertEquals("John Smith", manager[0]);
|
|
|
|
|
assertEquals("Sales", manager[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenResultConstructorInSelect_ThenListOfResultIsReturned() {
|
2018-09-11 10:46:05 +01:00
|
|
|
Query<Result> query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) "
|
|
|
|
|
+ "from DeptEmployee m");
|
2018-09-05 17:27:25 +01:00
|
|
|
List<Result> results = query.list();
|
|
|
|
|
Result result = results.get(0);
|
|
|
|
|
assertEquals("John Smith", result.getEmployeeName());
|
|
|
|
|
assertEquals("Sales", result.getDepartmentName());
|
2018-09-04 00:01:25 +01:00
|
|
|
}
|
2018-09-05 00:13:08 +01:00
|
|
|
|
|
|
|
|
@Test
|
2018-09-05 17:27:25 +01:00
|
|
|
public void whenResultTransformerOnQuery_ThenListOfResultIsReturned() {
|
2018-09-11 10:46:05 +01:00
|
|
|
Query query = session.createQuery("select m.name as employeeName, m.department.name as departmentName "
|
|
|
|
|
+ "from com.baeldung.hibernate.entities.DeptEmployee m");
|
2018-09-05 17:27:25 +01:00
|
|
|
query.setResultTransformer(Transformers.aliasToBean(Result.class));
|
|
|
|
|
List<Result> results = query.list();
|
|
|
|
|
Result result = results.get(0);
|
|
|
|
|
assertEquals("John Smith", result.getEmployeeName());
|
|
|
|
|
assertEquals("Sales", result.getDepartmentName());
|
2018-09-05 00:13:08 +01:00
|
|
|
}
|
2018-09-04 00:01:25 +01:00
|
|
|
}
|