Renamed Manager to DeptEmployee
This commit is contained in:
parent
a50baf1be3
commit
dbdbe79910
|
@ -5,6 +5,7 @@ import java.io.IOException;
|
|||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.baeldung.hibernate.entities.DeptEmployee;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
|
@ -92,7 +93,7 @@ public class HibernateUtil {
|
|||
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Manager.class);
|
||||
metadataSources.addAnnotatedClass(DeptEmployee.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
|
||||
|
||||
Metadata metadata = metadataSources.buildMetadata();
|
||||
|
|
|
@ -2,17 +2,18 @@ package com.baeldung.hibernate.entities;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Department {
|
||||
@Id
|
||||
long id;
|
||||
String name;
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy="department")
|
||||
List<Manager> employees;
|
||||
private List<DeptEmployee> employees;
|
||||
|
||||
public Department(String name) {
|
||||
this.name = name;
|
||||
|
@ -21,19 +22,24 @@ public class Department {
|
|||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public List<Manager> getEmployees() {
|
||||
|
||||
public List<DeptEmployee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
public void setEmployees(List<Manager> employees) {
|
||||
|
||||
public void setEmployees(List<DeptEmployee> employees) {
|
||||
this.employees = employees;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,23 @@
|
|||
package com.baeldung.hibernate.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Manager {
|
||||
public class DeptEmployee {
|
||||
@Id
|
||||
long id;
|
||||
String employeeNumber;
|
||||
String name;
|
||||
@ManyToOne
|
||||
Department department;
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private long id;
|
||||
|
||||
private String employeeNumber;
|
||||
|
||||
private String designation;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
private Department department;
|
||||
|
||||
public Manager(String name, String employeeNumber, Department department) {
|
||||
public DeptEmployee(String name, String employeeNumber, Department department) {
|
||||
this.name = name;
|
||||
this.employeeNumber = employeeNumber;
|
||||
this.department = department;
|
||||
|
@ -22,25 +26,40 @@ public class Manager {
|
|||
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,8 +1,9 @@
|
|||
package com.baeldung.hibernate.pojo;
|
||||
|
||||
public class Result {
|
||||
String employeeName;
|
||||
String departmentName;
|
||||
private String employeeName;
|
||||
|
||||
private String departmentName;
|
||||
|
||||
public Result(String employeeName, String departmentName) {
|
||||
this.employeeName = employeeName;
|
||||
|
|
|
@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.hibernate.entities.DeptEmployee;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.query.Query;
|
||||
|
@ -13,10 +14,9 @@ import org.junit.jupiter.api.BeforeEach;
|
|||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.hibernate.entities.Department;
|
||||
import com.baeldung.hibernate.entities.Manager;
|
||||
import com.baeldung.hibernate.pojo.Result;
|
||||
|
||||
class CustomClassIntegrationTest {
|
||||
public class CustomClassIntegrationTest {
|
||||
|
||||
private Session session;
|
||||
|
||||
|
@ -29,7 +29,7 @@ class CustomClassIntegrationTest {
|
|||
session.createNativeQuery("delete from manager").executeUpdate();
|
||||
session.createNativeQuery("delete from department").executeUpdate();
|
||||
Department department = new Department("Sales");
|
||||
Manager employee = new Manager("John Smith", "001", department);
|
||||
DeptEmployee employee = new DeptEmployee("John Smith", "001", department);
|
||||
session.persist(department);
|
||||
session.persist(employee);
|
||||
transaction.commit();
|
||||
|
@ -38,16 +38,16 @@ class CustomClassIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void whenAllManagersAreSelected_ThenObjectGraphIsReturned() {
|
||||
Query<Manager> query = session.createQuery("from com.baeldung.hibernate.entities.Manager");
|
||||
List<Manager> managers = query.list();
|
||||
Manager manager = managers.get(0);
|
||||
assertEquals("John Smith", manager.getName());
|
||||
assertEquals("Sales", manager.getDepartment().getName());
|
||||
Query<DeptEmployee> query = session.createQuery("from com.baeldung.hibernate.entities.DeptEmployee");
|
||||
List<DeptEmployee> deptEmployees = query.list();
|
||||
DeptEmployee deptEmployee = deptEmployees.get(0);
|
||||
assertEquals("John Smith", deptEmployee.getName());
|
||||
assertEquals("Sales", deptEmployee.getDepartment().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIndividualPropertiesAreSelected_ThenObjectArrayIsReturned() {
|
||||
Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.Manager m");
|
||||
Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.DeptEmployee m");
|
||||
List managers = query.list();
|
||||
Object[] manager = (Object[]) managers.get(0);
|
||||
assertEquals("John Smith", manager[0]);
|
||||
|
@ -56,7 +56,8 @@ class CustomClassIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void whenResultConstructorInSelect_ThenListOfResultIsReturned() {
|
||||
Query<Result> query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) from Manager m");
|
||||
Query<Result> query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) "
|
||||
+ "from DeptEmployee m");
|
||||
List<Result> results = query.list();
|
||||
Result result = results.get(0);
|
||||
assertEquals("John Smith", result.getEmployeeName());
|
||||
|
@ -65,7 +66,8 @@ class CustomClassIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void whenResultTransformerOnQuery_ThenListOfResultIsReturned() {
|
||||
Query query = session.createQuery("select m.name as employeeName, m.department.name as departmentName from com.baeldung.hibernate.entities.Manager m");
|
||||
Query query = session.createQuery("select m.name as employeeName, m.department.name as departmentName "
|
||||
+ "from com.baeldung.hibernate.entities.DeptEmployee m");
|
||||
query.setResultTransformer(Transformers.aliasToBean(Result.class));
|
||||
List<Result> results = query.list();
|
||||
Result result = results.get(0);
|
||||
|
|
Loading…
Reference in New Issue