Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
priyeshmashelkar committed Sep 5, 2018
1 parent fbe433e commit a50baf1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 37 deletions.
46 changes: 35 additions & 11 deletions hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
package com.baeldung.hibernate;

import com.baeldung.hibernate.pessimisticlocking.Individual;
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
import com.baeldung.hibernate.pojo.*;
import com.baeldung.hibernate.pojo.Person;
import com.baeldung.hibernate.pojo.inheritance.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import com.baeldung.hibernate.pessimisticlocking.Individual;
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
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 {
private static SessionFactory sessionFactory;
Expand Down Expand Up @@ -70,6 +92,8 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
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(com.baeldung.hibernate.entities.Department.class);

Metadata metadata = metadataSources.buildMetadata();
return metadata.getSessionFactoryBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
public class Department {
@Id
long id;
String name;
@OneToMany(mappedBy="department")
List<Employee> employees;
List<Manager> employees;

public Department(String name) {
this.name = name;
}

public long getId() {
return id;
}
Expand All @@ -26,10 +30,10 @@ public String getName() {
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
public List<Manager> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
public void setEmployees(List<Manager> employees) {
this.employees = employees;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
public class Employee {
public class Manager {
@Id
long id;
String employeeNumber;
String name;
String designation;
@ManyToOne
Department department;

public Manager(String name, String employeeNumber, Department department) {
this.name = name;
this.employeeNumber = employeeNumber;
this.department = department;
}

public long getId() {
return id;
}
Expand All @@ -33,12 +37,6 @@ public String getName() {
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Department getDepartment() {
return department;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public Result(String employeeName, String departmentName) {
this.departmentName = departmentName;
}

public Result() {
}

public String getEmployeeName() {
return employeeName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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.util.List;
import java.util.TimeZone;

import org.hibernate.Session;
import org.hibernate.Transaction;
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.Test;

import com.baeldung.hibernate.entities.Department;
import com.baeldung.hibernate.entities.Manager;
import com.baeldung.hibernate.pojo.Result;

class CustomClassIntegrationTest {
Expand All @@ -25,23 +26,50 @@ class CustomClassIntegrationTest {
public void setUp() throws IOException {
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.createNativeQuery("delete from emp").executeUpdate();
session.createNativeQuery("delete from dept").executeUpdate();
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);
session.persist(department);
session.persist(employee);
transaction.commit();
transaction = session.beginTransaction();
}

@Test
public void whenAllEmployeesSelected_ThenObjectGraphReturned() {
@SuppressWarnings("unchecked")
Query<Object> query = session.createQuery("from Employee");
List employees = query.list();
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());
}

@Test
public void whenIndividualPropertiesAreSelected_ThenObjectArrayIsReturned() {
Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.Manager m");
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 whenResultConstructorInSelect_ThenListOfResultReturned() {
Query query = session.createQuery("select new Result(e.name, e.department.name) from Employee e");
List<Result> employees = query.list();
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());
}
}

0 comments on commit a50baf1

Please sign in to comment.