From e2eeb795cd2b5ea4cc45f8ad7df049fb48940a54 Mon Sep 17 00:00:00 2001 From: Priyesh Mashelkar Date: Thu, 6 Dec 2018 20:03:26 +0000 Subject: [PATCH] BAEL-2344 Moved files to new hibernate5 module (#5854) * Added writer * Added implementation and test class * Added more details * Updated tests * Updated code as per review comments * Added test class and one named query * Updated test class * Added update HQL * Added new initialisation script and new queries * Corrected queries * Removed commented code * Added implementation and test class * Added more details * Updated tests * Updated code as per review comments * Added test class and one named query * Updated test class * Added update HQL * Added new initialisation script and new queries * Corrected queries * Removed commented code * BAEL-2344 Moved classes to new hibernate5 module * BAEL-2344 Moved files to new hibernate5 module --- .../src/main/resources/init_database.sql | 10 ++ .../hibernate/NamedQueryIntegrationTest.java | 98 +++++++++++++++++++ .../resources/hibernate-namedquery.properties | 9 ++ .../hibernate5/transaction.log | 0 4 files changed, 117 insertions(+) create mode 100644 persistence-modules/hibernate5/src/main/resources/init_database.sql create mode 100644 persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/NamedQueryIntegrationTest.java create mode 100644 persistence-modules/hibernate5/src/test/resources/hibernate-namedquery.properties create mode 100644 persistence-modules/hibernate5/transaction.log diff --git a/persistence-modules/hibernate5/src/main/resources/init_database.sql b/persistence-modules/hibernate5/src/main/resources/init_database.sql new file mode 100644 index 0000000000..b2848aa256 --- /dev/null +++ b/persistence-modules/hibernate5/src/main/resources/init_database.sql @@ -0,0 +1,10 @@ +CREATE ALIAS UPDATE_EMPLOYEE_DESIGNATION AS $$ +import java.sql.CallableStatement; +import java.sql.Connection; +import java.sql.SQLException; +@CODE +void updateEmployeeDesignation(final Connection conn, final String employeeNumber, final String title) throws SQLException { + CallableStatement updateStatement = conn.prepareCall("update deptemployee set title = '" + title + "' where employeeNumber = '" + employeeNumber + "'"); + updateStatement.execute(); +} +$$; \ No newline at end of file diff --git a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/NamedQueryIntegrationTest.java b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/NamedQueryIntegrationTest.java new file mode 100644 index 0000000000..cb73fe348c --- /dev/null +++ b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/NamedQueryIntegrationTest.java @@ -0,0 +1,98 @@ +package com.baeldung.hibernate; + +import com.baeldung.hibernate.entities.Department; +import com.baeldung.hibernate.entities.DeptEmployee; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.query.NativeQuery; +import org.hibernate.query.Query; +import org.junit.*; + +import java.io.IOException; + +public class NamedQueryIntegrationTest { + private static Session session; + + private Transaction transaction; + + private Long purchaseDeptId; + + @BeforeClass + public static void setUpClass() throws IOException { + session = HibernateUtil.getSessionFactory("hibernate-namedquery.properties").openSession(); + } + + @Before + public void setUp() throws IOException { + transaction = session.beginTransaction(); + session.createNativeQuery("delete from deptemployee").executeUpdate(); + session.createNativeQuery("delete from department").executeUpdate(); + Department salesDepartment = new Department("Sales"); + Department purchaseDepartment = new Department("Purchase"); + DeptEmployee employee1 = new DeptEmployee("John Wayne", "001", salesDepartment); + DeptEmployee employee2 = new DeptEmployee("Sarah Vinton", "002", salesDepartment); + DeptEmployee employee3 = new DeptEmployee("Lisa Carter", "003", salesDepartment); + session.persist(salesDepartment); + session.persist(purchaseDepartment); + purchaseDeptId = purchaseDepartment.getId(); + session.persist(employee1); + session.persist(employee2); + session.persist(employee3); + transaction.commit(); + transaction = session.beginTransaction(); + } + + @After + public void tearDown() { + if(transaction.isActive()) { + transaction.rollback(); + } + } + + @Test + public void whenNamedQueryIsCalledUsingCreateNamedQuery_ThenOk() { + Query query = session.createNamedQuery("DeptEmployee_FindByEmployeeNumber", DeptEmployee.class); + query.setParameter("employeeNo", "001"); + DeptEmployee result = query.getSingleResult(); + Assert.assertNotNull(result); + Assert.assertEquals("John Wayne", result.getName()); + } + + @Test + public void whenNamedNativeQueryIsCalledUsingCreateNamedQuery_ThenOk() { + Query query = session.createNamedQuery("DeptEmployee_FindByEmployeeName", DeptEmployee.class); + query.setParameter("name", "John Wayne"); + DeptEmployee result = query.getSingleResult(); + Assert.assertNotNull(result); + Assert.assertEquals("001", result.getEmployeeNumber()); + } + + @Test + public void whenNamedNativeQueryIsCalledUsingGetNamedNativeQuery_ThenOk() { + @SuppressWarnings("rawtypes") + NativeQuery query = session.getNamedNativeQuery("DeptEmployee_FindByEmployeeName"); + query.setParameter("name", "John Wayne"); + DeptEmployee result = (DeptEmployee) query.getSingleResult(); + Assert.assertNotNull(result); + Assert.assertEquals("001", result.getEmployeeNumber()); + } + + @Test + public void whenUpdateQueryIsCalledWithCreateNamedQuery_ThenOk() { + Query spQuery = session.createNamedQuery("DeptEmployee_UpdateEmployeeDepartment"); + spQuery.setParameter("employeeNo", "001"); + Department newDepartment = session.find(Department.class, purchaseDeptId); + spQuery.setParameter("newDepartment", newDepartment); + spQuery.executeUpdate(); + transaction.commit(); + } + + @Test + public void whenNamedStoredProcedureIsCalledWithCreateNamedQuery_ThenOk() { + Query spQuery = session.createNamedQuery("DeptEmployee_UpdateEmployeeDesignation"); + spQuery.setParameter("employeeNumber", "002"); + spQuery.setParameter("newDesignation", "Supervisor"); + spQuery.executeUpdate(); + transaction.commit(); + } +} diff --git a/persistence-modules/hibernate5/src/test/resources/hibernate-namedquery.properties b/persistence-modules/hibernate5/src/test/resources/hibernate-namedquery.properties new file mode 100644 index 0000000000..457f965347 --- /dev/null +++ b/persistence-modules/hibernate5/src/test/resources/hibernate-namedquery.properties @@ -0,0 +1,9 @@ +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'src/main/resources/init_database.sql' +hibernate.connection.username=sa +hibernate.connection.autocommit=true +jdbc.password= + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/persistence-modules/hibernate5/transaction.log b/persistence-modules/hibernate5/transaction.log new file mode 100644 index 0000000000..e69de29bb2