BAEL-2344 Removed unused code (#5860)
* Added implementation and test class * Added more details * Updated tests * Added implementation and test class * Added more details * Updated tests * Updated code as per review comments * BAEL-2344 Moved files to new hibernate5 module
This commit is contained in:
parent
18ff18671b
commit
5a87e036fa
@ -1,75 +0,0 @@
|
|||||||
package com.baeldung.hibernate.entities;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.ManyToOne;
|
|
||||||
|
|
||||||
@org.hibernate.annotations.NamedQueries({ @org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindByEmployeeNumber", query = "from DeptEmployee where employeeNumber = :employeeNo"),
|
|
||||||
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDesgination", query = "from DeptEmployee where designation = :designation"),
|
|
||||||
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_UpdateEmployeeDepartment", query = "Update DeptEmployee set department = :newDepartment where employeeNumber = :employeeNo"),
|
|
||||||
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDepartment", query = "from DeptEmployee where department = :department", timeout = 1, fetchSize = 10) })
|
|
||||||
@org.hibernate.annotations.NamedNativeQueries({ @org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_FindByEmployeeName", query = "select * from deptemployee emp where name=:name", resultClass = DeptEmployee.class),
|
|
||||||
@org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_UpdateEmployeeDesignation", query = "call UPDATE_EMPLOYEE_DESIGNATION(:employeeNumber, :newDesignation)", resultClass = DeptEmployee.class) })
|
|
||||||
@Entity
|
|
||||||
public class DeptEmployee {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
|
||||||
private long id;
|
|
||||||
|
|
||||||
private String employeeNumber;
|
|
||||||
|
|
||||||
private String designation;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
private Department department;
|
|
||||||
|
|
||||||
public DeptEmployee(String name, String employeeNumber, Department department) {
|
|
||||||
this.name = name;
|
|
||||||
this.employeeNumber = employeeNumber;
|
|
||||||
this.department = department;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEmployeeNumber() {
|
|
||||||
return employeeNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEmployeeNumber(String employeeNumber) {
|
|
||||||
this.employeeNumber = employeeNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Department getDepartment() {
|
|
||||||
return department;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDepartment(Department department) {
|
|
||||||
this.department = department;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDesignation() {
|
|
||||||
return designation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDesignation(String designation) {
|
|
||||||
this.designation = designation;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
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 designation) throws SQLException {
|
|
||||||
CallableStatement updateStatement = conn.prepareCall("update deptemployee set designation = '" + designation + "' where employeeNumber = '" + employeeNumber + "'");
|
|
||||||
updateStatement.execute();
|
|
||||||
}
|
|
||||||
$$;
|
|
@ -1,103 +0,0 @@
|
|||||||
package com.baeldung.hibernate;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.Transaction;
|
|
||||||
import org.hibernate.query.NativeQuery;
|
|
||||||
import org.hibernate.query.Query;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.baeldung.hibernate.entities.Department;
|
|
||||||
import com.baeldung.hibernate.entities.DeptEmployee;
|
|
||||||
|
|
||||||
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<DeptEmployee> 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<DeptEmployee> 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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
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
|
|
Loading…
x
Reference in New Issue
Block a user