Added tests
This commit is contained in:
parent
fbe433ebb4
commit
a50baf1be3
|
@ -1,12 +1,10 @@
|
||||||
package com.baeldung.hibernate;
|
package com.baeldung.hibernate;
|
||||||
|
|
||||||
import com.baeldung.hibernate.pessimisticlocking.Individual;
|
import java.io.FileInputStream;
|
||||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
|
import java.io.IOException;
|
||||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
|
import java.net.URL;
|
||||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
|
import java.util.Properties;
|
||||||
import com.baeldung.hibernate.pojo.*;
|
|
||||||
import com.baeldung.hibernate.pojo.Person;
|
|
||||||
import com.baeldung.hibernate.pojo.inheritance.*;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.Metadata;
|
import org.hibernate.boot.Metadata;
|
||||||
|
@ -14,10 +12,34 @@ import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import com.baeldung.hibernate.pessimisticlocking.Individual;
|
||||||
import java.io.IOException;
|
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
|
||||||
import java.net.URL;
|
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
|
||||||
import java.util.Properties;
|
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
|
||||||
|
import com.baeldung.hibernate.pojo.Course;
|
||||||
|
import com.baeldung.hibernate.pojo.Employee;
|
||||||
|
import com.baeldung.hibernate.pojo.EntityDescription;
|
||||||
|
import com.baeldung.hibernate.pojo.OrderEntry;
|
||||||
|
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
|
||||||
|
import com.baeldung.hibernate.pojo.OrderEntryPK;
|
||||||
|
import com.baeldung.hibernate.pojo.Person;
|
||||||
|
import com.baeldung.hibernate.pojo.Phone;
|
||||||
|
import com.baeldung.hibernate.pojo.PointEntity;
|
||||||
|
import com.baeldung.hibernate.pojo.PolygonEntity;
|
||||||
|
import com.baeldung.hibernate.pojo.Product;
|
||||||
|
import com.baeldung.hibernate.pojo.Student;
|
||||||
|
import com.baeldung.hibernate.pojo.TemporalValues;
|
||||||
|
import com.baeldung.hibernate.pojo.User;
|
||||||
|
import com.baeldung.hibernate.pojo.UserProfile;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.Animal;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.Bag;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.Book;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.Car;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.Pen;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.Pet;
|
||||||
|
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
|
||||||
|
|
||||||
public class HibernateUtil {
|
public class HibernateUtil {
|
||||||
private static SessionFactory sessionFactory;
|
private static SessionFactory sessionFactory;
|
||||||
|
@ -70,6 +92,8 @@ public class HibernateUtil {
|
||||||
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
|
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
|
||||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
|
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
|
||||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
|
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
|
||||||
|
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Manager.class);
|
||||||
|
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
|
||||||
|
|
||||||
Metadata metadata = metadataSources.buildMetadata();
|
Metadata metadata = metadataSources.buildMetadata();
|
||||||
return metadata.getSessionFactoryBuilder()
|
return metadata.getSessionFactoryBuilder()
|
||||||
|
|
|
@ -5,7 +5,6 @@ import java.util.List;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.Table;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Department {
|
public class Department {
|
||||||
|
@ -13,7 +12,12 @@ public class Department {
|
||||||
long id;
|
long id;
|
||||||
String name;
|
String name;
|
||||||
@OneToMany(mappedBy="department")
|
@OneToMany(mappedBy="department")
|
||||||
List<Employee> employees;
|
List<Manager> employees;
|
||||||
|
|
||||||
|
public Department(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -26,10 +30,10 @@ public class Department {
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
public List<Employee> getEmployees() {
|
public List<Manager> getEmployees() {
|
||||||
return employees;
|
return employees;
|
||||||
}
|
}
|
||||||
public void setEmployees(List<Employee> employees) {
|
public void setEmployees(List<Manager> employees) {
|
||||||
this.employees = employees;
|
this.employees = employees;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,18 +3,22 @@ package com.baeldung.hibernate.entities;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.Table;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Employee {
|
public class Manager {
|
||||||
@Id
|
@Id
|
||||||
long id;
|
long id;
|
||||||
String employeeNumber;
|
String employeeNumber;
|
||||||
String name;
|
String name;
|
||||||
String designation;
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
Department department;
|
Department department;
|
||||||
|
|
||||||
|
public Manager(String name, String employeeNumber, Department department) {
|
||||||
|
this.name = name;
|
||||||
|
this.employeeNumber = employeeNumber;
|
||||||
|
this.department = department;
|
||||||
|
}
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -33,12 +37,6 @@ public class Employee {
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
public String getDesignation() {
|
|
||||||
return designation;
|
|
||||||
}
|
|
||||||
public void setDesignation(String designation) {
|
|
||||||
this.designation = designation;
|
|
||||||
}
|
|
||||||
public Department getDepartment() {
|
public Department getDepartment() {
|
||||||
return department;
|
return department;
|
||||||
}
|
}
|
|
@ -9,6 +9,9 @@ public class Result {
|
||||||
this.departmentName = departmentName;
|
this.departmentName = departmentName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Result() {
|
||||||
|
}
|
||||||
|
|
||||||
public String getEmployeeName() {
|
public String getEmployeeName() {
|
||||||
return employeeName;
|
return employeeName;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
package com.baeldung.hibernate;
|
package com.baeldung.hibernate;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.TimeZone;
|
|
||||||
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
import org.hibernate.query.Query;
|
import org.hibernate.query.Query;
|
||||||
import org.junit.Before;
|
import org.hibernate.transform.Transformers;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.entities.Department;
|
||||||
|
import com.baeldung.hibernate.entities.Manager;
|
||||||
import com.baeldung.hibernate.pojo.Result;
|
import com.baeldung.hibernate.pojo.Result;
|
||||||
|
|
||||||
class CustomClassIntegrationTest {
|
class CustomClassIntegrationTest {
|
||||||
|
@ -25,23 +26,50 @@ class CustomClassIntegrationTest {
|
||||||
public void setUp() throws IOException {
|
public void setUp() throws IOException {
|
||||||
session = HibernateUtil.getSessionFactory().openSession();
|
session = HibernateUtil.getSessionFactory().openSession();
|
||||||
transaction = session.beginTransaction();
|
transaction = session.beginTransaction();
|
||||||
session.createNativeQuery("delete from emp").executeUpdate();
|
session.createNativeQuery("delete from manager").executeUpdate();
|
||||||
session.createNativeQuery("delete from dept").executeUpdate();
|
session.createNativeQuery("delete from department").executeUpdate();
|
||||||
|
Department department = new Department("Sales");
|
||||||
|
Manager employee = new Manager("John Smith", "001", department);
|
||||||
|
session.persist(department);
|
||||||
|
session.persist(employee);
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
transaction = session.beginTransaction();
|
transaction = session.beginTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAllEmployeesSelected_ThenObjectGraphReturned() {
|
public void whenAllManagersAreSelected_ThenObjectGraphIsReturned() {
|
||||||
@SuppressWarnings("unchecked")
|
Query<Manager> query = session.createQuery("from com.baeldung.hibernate.entities.Manager");
|
||||||
Query<Object> query = session.createQuery("from Employee");
|
List<Manager> managers = query.list();
|
||||||
List employees = query.list();
|
Manager manager = managers.get(0);
|
||||||
|
assertEquals("John Smith", manager.getName());
|
||||||
|
assertEquals("Sales", manager.getDepartment().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenResultConstructorInSelect_ThenListOfResultReturned() {
|
public void whenIndividualPropertiesAreSelected_ThenObjectArrayIsReturned() {
|
||||||
Query query = session.createQuery("select new Result(e.name, e.department.name) from Employee e");
|
Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.Manager m");
|
||||||
List<Result> employees = query.list();
|
List managers = query.list();
|
||||||
|
Object[] manager = (Object[]) managers.get(0);
|
||||||
|
assertEquals("John Smith", manager[0]);
|
||||||
|
assertEquals("Sales", manager[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
List<Result> results = query.list();
|
||||||
|
Result result = results.get(0);
|
||||||
|
assertEquals("John Smith", result.getEmployeeName());
|
||||||
|
assertEquals("Sales", result.getDepartmentName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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.setResultTransformer(Transformers.aliasToBean(Result.class));
|
||||||
|
List<Result> results = query.list();
|
||||||
|
Result result = results.get(0);
|
||||||
|
assertEquals("John Smith", result.getEmployeeName());
|
||||||
|
assertEquals("Sales", result.getDepartmentName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue