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 12a9bda5f9..fec7b04462 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java @@ -5,6 +5,7 @@ import java.util.List; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; +import javax.persistence.Table; @Entity public class Department { @@ -13,4 +14,22 @@ public class Department { String name; @OneToMany(mappedBy="department") List employees; + 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() { + return employees; + } + public void setEmployees(List employees) { + this.employees = employees; + } } diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Employee.java index 8b5c554c44..eec7c54d93 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Employee.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Employee.java @@ -3,6 +3,7 @@ package com.baeldung.hibernate.entities; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; +import javax.persistence.Table; @Entity public class Employee { diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java new file mode 100644 index 0000000000..6893060dd8 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Result.java @@ -0,0 +1,27 @@ +package com.baeldung.hibernate.pojo; + +public class Result { + String employeeName; + String departmentName; + + public Result(String employeeName, String departmentName) { + this.employeeName = employeeName; + this.departmentName = departmentName; + } + + public String getEmployeeName() { + return employeeName; + } + + public void setEmployeeName(String employeeName) { + this.employeeName = employeeName; + } + + public String getDepartmentName() { + return departmentName; + } + + public void setDepartmentName(String departmentName) { + this.departmentName = departmentName; + } +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java index 8b2fd4854d..c2ccef5c85 100644 --- a/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java +++ b/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java @@ -10,27 +10,38 @@ import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; import org.junit.Before; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import com.baeldung.hibernate.pojo.Result; + class CustomClassIntegrationTest { private Session session; private Transaction transaction; - @Before + @BeforeEach public void setUp() throws IOException { session = HibernateUtil.getSessionFactory().openSession(); transaction = session.beginTransaction(); - session.createNativeQuery("delete from employee").executeUpdate(); - session.createNativeQuery("delete from department").executeUpdate(); + session.createNativeQuery("delete from emp").executeUpdate(); + session.createNativeQuery("delete from dept").executeUpdate(); transaction.commit(); + transaction = session.beginTransaction(); } @Test - void whenAllEmployeesSelected_ThenObjectGraphReturned() { - Query query = session.createQuery("from employee"); + public void whenAllEmployeesSelected_ThenObjectGraphReturned() { + @SuppressWarnings("unchecked") + Query query = session.createQuery("from Employee"); List employees = query.list(); } + + @Test + public void whenResultConstructorInSelect_ThenListOfResultReturned() { + Query query = session.createQuery("select new Result(e.name, e.department.name) from Employee e"); + List employees = query.list(); + } }