diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java index c75f8e358f..849d271cd4 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java @@ -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(); diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java index ceabfc742e..ff94f4f849 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java @@ -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 employees; + private List 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 getEmployees() { + + public List getEmployees() { return employees; } - public void setEmployees(List employees) { + + public void setEmployees(List employees) { this.employees = employees; } } diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Manager.java b/hibernate5/src/main/java/com/baeldung/hibernate/entities/DeptEmployee.java similarity index 59% rename from hibernate5/src/main/java/com/baeldung/hibernate/entities/Manager.java rename to hibernate5/src/main/java/com/baeldung/hibernate/entities/DeptEmployee.java index d9d8e8d40f..7a51009b62 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Manager.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/entities/DeptEmployee.java @@ -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; + } } diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java index 588a895c14..607269a267 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java @@ -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; diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java index 622063b828..29ae55b773 100644 --- a/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java +++ b/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java @@ -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 query = session.createQuery("from com.baeldung.hibernate.entities.Manager"); - List managers = query.list(); - Manager manager = managers.get(0); - assertEquals("John Smith", manager.getName()); - assertEquals("Sales", manager.getDepartment().getName()); + Query query = session.createQuery("from com.baeldung.hibernate.entities.DeptEmployee"); + List 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 query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) from Manager m"); + Query query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) " + + "from DeptEmployee m"); List 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 results = query.list(); Result result = results.get(0);