Skip to content

Commit e2eeb79

Browse files
priyeshmashelkarmaibin
authored andcommitted
BAEL-2344 Moved files to new hibernate5 module (eugenp#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
1 parent d78915c commit e2eeb79

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE ALIAS UPDATE_EMPLOYEE_DESIGNATION AS $$
2+
import java.sql.CallableStatement;
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
@CODE
6+
void updateEmployeeDesignation(final Connection conn, final String employeeNumber, final String title) throws SQLException {
7+
CallableStatement updateStatement = conn.prepareCall("update deptemployee set title = '" + title + "' where employeeNumber = '" + employeeNumber + "'");
8+
updateStatement.execute();
9+
}
10+
$$;
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.hibernate;
2+
3+
import com.baeldung.hibernate.entities.Department;
4+
import com.baeldung.hibernate.entities.DeptEmployee;
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.*;
10+
11+
import java.io.IOException;
12+
13+
public class NamedQueryIntegrationTest {
14+
private static Session session;
15+
16+
private Transaction transaction;
17+
18+
private Long purchaseDeptId;
19+
20+
@BeforeClass
21+
public static void setUpClass() throws IOException {
22+
session = HibernateUtil.getSessionFactory("hibernate-namedquery.properties").openSession();
23+
}
24+
25+
@Before
26+
public void setUp() throws IOException {
27+
transaction = session.beginTransaction();
28+
session.createNativeQuery("delete from deptemployee").executeUpdate();
29+
session.createNativeQuery("delete from department").executeUpdate();
30+
Department salesDepartment = new Department("Sales");
31+
Department purchaseDepartment = new Department("Purchase");
32+
DeptEmployee employee1 = new DeptEmployee("John Wayne", "001", salesDepartment);
33+
DeptEmployee employee2 = new DeptEmployee("Sarah Vinton", "002", salesDepartment);
34+
DeptEmployee employee3 = new DeptEmployee("Lisa Carter", "003", salesDepartment);
35+
session.persist(salesDepartment);
36+
session.persist(purchaseDepartment);
37+
purchaseDeptId = purchaseDepartment.getId();
38+
session.persist(employee1);
39+
session.persist(employee2);
40+
session.persist(employee3);
41+
transaction.commit();
42+
transaction = session.beginTransaction();
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
if(transaction.isActive()) {
48+
transaction.rollback();
49+
}
50+
}
51+
52+
@Test
53+
public void whenNamedQueryIsCalledUsingCreateNamedQuery_ThenOk() {
54+
Query<DeptEmployee> query = session.createNamedQuery("DeptEmployee_FindByEmployeeNumber", DeptEmployee.class);
55+
query.setParameter("employeeNo", "001");
56+
DeptEmployee result = query.getSingleResult();
57+
Assert.assertNotNull(result);
58+
Assert.assertEquals("John Wayne", result.getName());
59+
}
60+
61+
@Test
62+
public void whenNamedNativeQueryIsCalledUsingCreateNamedQuery_ThenOk() {
63+
Query<DeptEmployee> query = session.createNamedQuery("DeptEmployee_FindByEmployeeName", DeptEmployee.class);
64+
query.setParameter("name", "John Wayne");
65+
DeptEmployee result = query.getSingleResult();
66+
Assert.assertNotNull(result);
67+
Assert.assertEquals("001", result.getEmployeeNumber());
68+
}
69+
70+
@Test
71+
public void whenNamedNativeQueryIsCalledUsingGetNamedNativeQuery_ThenOk() {
72+
@SuppressWarnings("rawtypes")
73+
NativeQuery query = session.getNamedNativeQuery("DeptEmployee_FindByEmployeeName");
74+
query.setParameter("name", "John Wayne");
75+
DeptEmployee result = (DeptEmployee) query.getSingleResult();
76+
Assert.assertNotNull(result);
77+
Assert.assertEquals("001", result.getEmployeeNumber());
78+
}
79+
80+
@Test
81+
public void whenUpdateQueryIsCalledWithCreateNamedQuery_ThenOk() {
82+
Query spQuery = session.createNamedQuery("DeptEmployee_UpdateEmployeeDepartment");
83+
spQuery.setParameter("employeeNo", "001");
84+
Department newDepartment = session.find(Department.class, purchaseDeptId);
85+
spQuery.setParameter("newDepartment", newDepartment);
86+
spQuery.executeUpdate();
87+
transaction.commit();
88+
}
89+
90+
@Test
91+
public void whenNamedStoredProcedureIsCalledWithCreateNamedQuery_ThenOk() {
92+
Query spQuery = session.createNamedQuery("DeptEmployee_UpdateEmployeeDesignation");
93+
spQuery.setParameter("employeeNumber", "002");
94+
spQuery.setParameter("newDesignation", "Supervisor");
95+
spQuery.executeUpdate();
96+
transaction.commit();
97+
}
98+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
hibernate.connection.driver_class=org.h2.Driver
2+
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'src/main/resources/init_database.sql'
3+
hibernate.connection.username=sa
4+
hibernate.connection.autocommit=true
5+
jdbc.password=
6+
7+
hibernate.dialect=org.hibernate.dialect.H2Dialect
8+
hibernate.show_sql=true
9+
hibernate.hbm2ddl.auto=create-drop

persistence-modules/hibernate5/transaction.log

Whitespace-only changes.

0 commit comments

Comments
 (0)