Class Refactor (#5374)

New tests in Hibernate with proxy
This commit is contained in:
Felipe Santiago Corro 2018-10-01 23:42:46 -03:00 committed by maibin
parent ea22559064
commit 8a5d9843f6
5 changed files with 105 additions and 108 deletions

View File

@ -1,56 +0,0 @@
package com.baeldung.hibernate.proxy;
import org.hibernate.annotations.BatchSize;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@BatchSize(size = 5)
public class BatchEmployee implements Serializable {
@Id
@GeneratedValue (strategy = GenerationType.SEQUENCE)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Boss boss;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@ -2,9 +2,10 @@ package com.baeldung.hibernate.proxy;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
@Entity
public class Boss implements Serializable {
public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@ -13,14 +14,10 @@ public class Boss implements Serializable {
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Company() { }
public Boss() { }
public Boss(String name, String surname) {
public Company(String name) {
this.name = name;
this.surname = surname;
}
public Long getId() {
@ -39,11 +36,17 @@ public class Boss implements Serializable {
this.name = name;
}
public String getSurname() {
return surname;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Company company = (Company) o;
return Objects.equals(id, company.id) &&
Objects.equals(name, company.name);
}
public void setSurname(String surname) {
this.surname = surname;
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}

View File

@ -1,9 +1,13 @@
package com.baeldung.hibernate.proxy;
import org.hibernate.annotations.BatchSize;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
@Entity
@BatchSize(size = 5)
public class Employee implements Serializable {
@Id
@ -11,13 +15,17 @@ public class Employee implements Serializable {
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Boss boss;
private Company workplace;
@Column(name = "name")
private String name;
@Column(name = "first_name")
private String firstName;
@Column(name = "surname")
private String surname;
public Employee() { }
public Employee(Company workplace, String firstName) {
this.workplace = workplace;
this.firstName = firstName;
}
public Long getId() {
return id;
@ -27,27 +35,34 @@ public class Employee implements Serializable {
this.id = id;
}
public Boss getBoss() {
return boss;
public Company getWorkplace() {
return workplace;
}
public void setBoss(Boss boss) {
this.boss = boss;
public void setWorkplace(Company workplace) {
this.workplace = workplace;
}
public String getName() {
return name;
public String getFirstName() {
return firstName;
}
public void setName(String name) {
this.name = name;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(id, employee.id) &&
Objects.equals(workplace, employee.workplace) &&
Objects.equals(firstName, employee.firstName);
}
public void setSurname(String surname) {
this.surname = surname;
@Override
public int hashCode() {
return Objects.hash(id, workplace, firstName);
}
}

View File

@ -30,7 +30,7 @@ public class HibernateUtil {
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
metadataSources.addPackage("com.baeldung.hibernate.proxy");
metadataSources.addAnnotatedClass(Boss.class);
metadataSources.addAnnotatedClass(Company.class);
metadataSources.addAnnotatedClass(Employee.class);
Metadata metadata = metadataSources.buildMetadata();

View File

@ -1,6 +1,7 @@
package com.baeldung.hibernate.proxy;
import org.hibernate.*;
import org.hibernate.proxy.HibernateProxy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -8,25 +9,31 @@ import org.junit.Test;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.fail;
public class HibernateProxyUnitTest {
private SessionFactory factory;
private Session session;
private Company workplace;
private Employee albert;
private Employee bob;
private Employee charlotte;
@Before
public void init(){
try {
session = HibernateUtil.getSessionFactory("hibernate.properties")
.openSession();
factory = HibernateUtil.getSessionFactory("hibernate.properties");
session = factory.openSession();
} catch (HibernateException | IOException e) {
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
}
Boss boss = new Boss("Eduard", "Freud");
session.save(boss);
}
@After
@ -36,40 +43,68 @@ public class HibernateProxyUnitTest {
}
}
@Test(expected = NullPointerException.class)
@Test
public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
Employee employee = session.get(Employee.class, new Long(14));
Employee employee = session.get(Employee.class, 14L);
assertNull(employee);
}
@Test(expected = ObjectNotFoundException.class)
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException() {
Employee employee = session.load(Employee.class, 999L);
assertNull(employee);
employee.getId();
}
@Test
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
Employee employee = session.load(Employee.class, new Long(14));
Employee employee = session.load(Employee.class, 14L);
assertNotNull(employee);
assertTrue(employee instanceof HibernateProxy);
}
@Test
public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() {
Transaction transaction = session.beginTransaction();
public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery() {
Transaction tx = session.beginTransaction();
for (long i = 1; i <= 5; i++) {
Employee employee = new Employee();
employee.setName("Employee " + i);
session.save(employee);
}
//We are saving 3 entities with one flush
this.workplace = new Company("Bizco");
session.save(workplace);
this.albert = new Employee(workplace, "Albert");
session.save(albert);
this.bob = new Employee(workplace, "Bob");
session.save(bob);
this.charlotte = new Employee(workplace, "Charlotte");
session.save(charlotte);
//After this line is possible to see all the insertions in the logs
session.flush();
session.clear();
transaction.commit();
transaction = session.beginTransaction();
tx.commit();
session = factory.openSession();
List<Employee> employeeList = session.createQuery("from Employee")
.setCacheMode(CacheMode.IGNORE).getResultList();
Employee proxyAlbert = session.load(Employee.class, this.albert.getId());
assertNotNull(proxyAlbert);
assertTrue(proxyAlbert instanceof HibernateProxy);
assertEquals(employeeList.size(), 5);
transaction.commit();
Employee proxyBob = session.load(Employee.class, this.bob.getId());
assertNotNull(proxyBob);
assertTrue(proxyBob instanceof HibernateProxy);
Employee proxyCharlotte = session.load(Employee.class, this.charlotte.getId());
assertNotNull(proxyCharlotte);
assertTrue(proxyCharlotte instanceof HibernateProxy);
//Fetching from database 3 entities with one call
//Select from log: where employee0_.id in (?, ?, ?)
proxyAlbert.getFirstName();
assertEquals(proxyAlbert, this.albert);
assertEquals(proxyBob, this.bob);
assertEquals(proxyCharlotte, this.charlotte);
}
}