Added Result

This commit is contained in:
Priyesh Mashelkar 2018-09-05 00:13:08 +01:00
parent 9efd6103ec
commit fbe433ebb4
4 changed files with 63 additions and 5 deletions

View File

@ -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<Employee> 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<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -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<Object> 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<Result> employees = query.list();
}
}