java-tutorials/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java

48 lines
1.4 KiB
Java
Raw Normal View History

2018-09-04 00:01:25 +01:00
package com.baeldung.hibernate;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.util.List;
import java.util.TimeZone;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.junit.Before;
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 00:13:08 +01:00
import com.baeldung.hibernate.pojo.Result;
2018-09-04 00:01:25 +01:00
class CustomClassIntegrationTest {
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 00:13:08 +01:00
session.createNativeQuery("delete from emp").executeUpdate();
session.createNativeQuery("delete from dept").executeUpdate();
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 00:13:08 +01:00
public void whenAllEmployeesSelected_ThenObjectGraphReturned() {
@SuppressWarnings("unchecked")
Query<Object> query = session.createQuery("from Employee");
2018-09-04 00:01:25 +01:00
List employees = query.list();
}
2018-09-05 00:13:08 +01:00
@Test
public void whenResultConstructorInSelect_ThenListOfResultReturned() {
Query query = session.createQuery("select new Result(e.name, e.department.name) from Employee e");
List<Result> employees = query.list();
}
2018-09-04 00:01:25 +01:00
}