Skip to content

Commit cac62f0

Browse files
priyeshmashelkarmaibin
authored andcommitted
BAEL-2344 Hibernate Named Query (eugenp#5835)
* 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 * printf examples Issue: BAEL-2228 * Update README.md * 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
1 parent a21f820 commit cac62f0

5 files changed

Lines changed: 210 additions & 3 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.hibernate.entities;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.ManyToOne;
8+
9+
@org.hibernate.annotations.NamedQueries({ @org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindByEmployeeNumber", query = "from DeptEmployee where employeeNumber = :employeeNo"),
10+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDesgination", query = "from DeptEmployee where designation = :designation"),
11+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_UpdateEmployeeDepartment", query = "Update DeptEmployee set department = :newDepartment where employeeNumber = :employeeNo"),
12+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDepartment", query = "from DeptEmployee where department = :department", timeout = 1, fetchSize = 10) })
13+
@org.hibernate.annotations.NamedNativeQueries({ @org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_FindByEmployeeName", query = "select * from deptemployee emp where name=:name", resultClass = DeptEmployee.class),
14+
@org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_UpdateEmployeeDesignation", query = "call UPDATE_EMPLOYEE_DESIGNATION(:employeeNumber, :newDesignation)", resultClass = DeptEmployee.class) })
15+
@Entity
16+
public class DeptEmployee {
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
19+
private long id;
20+
21+
private String employeeNumber;
22+
23+
private String designation;
24+
25+
private String name;
26+
27+
@ManyToOne
28+
private Department department;
29+
30+
public DeptEmployee(String name, String employeeNumber, Department department) {
31+
this.name = name;
32+
this.employeeNumber = employeeNumber;
33+
this.department = department;
34+
}
35+
36+
public long getId() {
37+
return id;
38+
}
39+
40+
public void setId(long id) {
41+
this.id = id;
42+
}
43+
44+
public String getEmployeeNumber() {
45+
return employeeNumber;
46+
}
47+
48+
public void setEmployeeNumber(String employeeNumber) {
49+
this.employeeNumber = employeeNumber;
50+
}
51+
52+
public String getName() {
53+
return name;
54+
}
55+
56+
public void setName(String name) {
57+
this.name = name;
58+
}
59+
60+
public Department getDepartment() {
61+
return department;
62+
}
63+
64+
public void setDepartment(Department department) {
65+
this.department = department;
66+
}
67+
68+
public String getDesignation() {
69+
return designation;
70+
}
71+
72+
public void setDesignation(String designation) {
73+
this.designation = designation;
74+
}
75+
}
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 designation) throws SQLException {
7+
CallableStatement updateStatement = conn.prepareCall("update deptemployee set designation = '" + designation + "' where employeeNumber = '" + employeeNumber + "'");
8+
updateStatement.execute();
9+
}
10+
$$;
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
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/src/main/java/com/baeldung/hibernate/entities/DeptEmployee.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.baeldung.hibernate.entities;
22

3-
import javax.persistence.*;
4-
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.ManyToOne;
8+
9+
@org.hibernate.annotations.NamedQueries({ @org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindByEmployeeNumber", query = "from DeptEmployee where employeeNumber = :employeeNo"),
10+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDesgination", query = "from DeptEmployee where designation = :designation"),
11+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_UpdateEmployeeDepartment", query = "Update DeptEmployee set department = :newDepartment where employeeNumber = :employeeNo"),
12+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDepartment", query = "from DeptEmployee where department = :department", timeout = 1, fetchSize = 10) })
13+
@org.hibernate.annotations.NamedNativeQueries({ @org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_FindByEmployeeName", query = "select * from deptemployee emp where name=:name", resultClass = DeptEmployee.class),
14+
@org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_UpdateEmployeeDesignation", query = "call UPDATE_EMPLOYEE_DESIGNATION(:employeeNumber, :newDesignation)", resultClass = DeptEmployee.class) })
515
@Entity
616
public class DeptEmployee {
717
@Id
@@ -16,7 +26,7 @@ public class DeptEmployee {
1626

1727
@ManyToOne
1828
private Department department;
19-
29+
2030
public DeptEmployee(String name, String employeeNumber, Department department) {
2131
this.name = name;
2232
this.employeeNumber = employeeNumber;

0 commit comments

Comments
 (0)