|
| 1 | +package com.baeldung.hibernate; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.hibernate.Session; |
| 6 | +import org.hibernate.Transaction; |
| 7 | +import org.hibernate.query.NativeQuery; |
| 8 | +import org.hibernate.query.Query; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import com.baeldung.hibernate.entities.Department; |
| 16 | +import com.baeldung.hibernate.entities.DeptEmployee; |
| 17 | + |
| 18 | +public class NamedQueryIntegrationTest { |
| 19 | + private static Session session; |
| 20 | + |
| 21 | + private Transaction transaction; |
| 22 | + |
| 23 | + private Long purchaseDeptId; |
| 24 | + |
| 25 | + @BeforeClass |
| 26 | + public static void setUpClass() throws IOException { |
| 27 | + session = HibernateUtil.getSessionFactory("hibernate-namedquery.properties").openSession(); |
| 28 | + } |
| 29 | + |
| 30 | + @Before |
| 31 | + public void setUp() throws IOException { |
| 32 | + transaction = session.beginTransaction(); |
| 33 | + session.createNativeQuery("delete from deptemployee").executeUpdate(); |
| 34 | + session.createNativeQuery("delete from department").executeUpdate(); |
| 35 | + Department salesDepartment = new Department("Sales"); |
| 36 | + Department purchaseDepartment = new Department("Purchase"); |
| 37 | + DeptEmployee employee1 = new DeptEmployee("John Wayne", "001", salesDepartment); |
| 38 | + DeptEmployee employee2 = new DeptEmployee("Sarah Vinton", "002", salesDepartment); |
| 39 | + DeptEmployee employee3 = new DeptEmployee("Lisa Carter", "003", salesDepartment); |
| 40 | + session.persist(salesDepartment); |
| 41 | + session.persist(purchaseDepartment); |
| 42 | + purchaseDeptId = purchaseDepartment.getId(); |
| 43 | + session.persist(employee1); |
| 44 | + session.persist(employee2); |
| 45 | + session.persist(employee3); |
| 46 | + transaction.commit(); |
| 47 | + transaction = session.beginTransaction(); |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void tearDown() { |
| 52 | + if(transaction.isActive()) { |
| 53 | + transaction.rollback(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void whenNamedQueryIsCalledUsingCreateNamedQuery_ThenOk() { |
| 59 | + Query<DeptEmployee> query = session.createNamedQuery("DeptEmployee_FindByEmployeeNumber", DeptEmployee.class); |
| 60 | + query.setParameter("employeeNo", "001"); |
| 61 | + DeptEmployee result = query.getSingleResult(); |
| 62 | + Assert.assertNotNull(result); |
| 63 | + Assert.assertEquals("John Wayne", result.getName()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void whenNamedNativeQueryIsCalledUsingCreateNamedQuery_ThenOk() { |
| 68 | + Query<DeptEmployee> query = session.createNamedQuery("DeptEmployee_FindByEmployeeName", DeptEmployee.class); |
| 69 | + query.setParameter("name", "John Wayne"); |
| 70 | + DeptEmployee result = query.getSingleResult(); |
| 71 | + Assert.assertNotNull(result); |
| 72 | + Assert.assertEquals("001", result.getEmployeeNumber()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void whenNamedNativeQueryIsCalledUsingGetNamedNativeQuery_ThenOk() { |
| 77 | + @SuppressWarnings("rawtypes") |
| 78 | + NativeQuery query = session.getNamedNativeQuery("DeptEmployee_FindByEmployeeName"); |
| 79 | + query.setParameter("name", "John Wayne"); |
| 80 | + DeptEmployee result = (DeptEmployee) query.getSingleResult(); |
| 81 | + Assert.assertNotNull(result); |
| 82 | + Assert.assertEquals("001", result.getEmployeeNumber()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void whenUpdateQueryIsCalledWithCreateNamedQuery_ThenOk() { |
| 87 | + Query spQuery = session.createNamedQuery("DeptEmployee_UpdateEmployeeDepartment"); |
| 88 | + spQuery.setParameter("employeeNo", "001"); |
| 89 | + Department newDepartment = session.find(Department.class, purchaseDeptId); |
| 90 | + spQuery.setParameter("newDepartment", newDepartment); |
| 91 | + spQuery.executeUpdate(); |
| 92 | + transaction.commit(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void whenNamedStoredProcedureIsCalledWithCreateNamedQuery_ThenOk() { |
| 97 | + Query spQuery = session.createNamedQuery("DeptEmployee_UpdateEmployeeDesignation"); |
| 98 | + spQuery.setParameter("employeeNumber", "002"); |
| 99 | + spQuery.setParameter("newDesignation", "Supervisor"); |
| 100 | + spQuery.executeUpdate(); |
| 101 | + transaction.commit(); |
| 102 | + } |
| 103 | +} |
0 commit comments