Polish Session.load Tests (#5378)
Cleaned up some confusing wording and a confusing test. Added two more tests for demonstrating how session.load works with associations. Issue: BAEL-2126
This commit is contained in:
parent
52c2ce4ede
commit
9a085b0e8f
|
@ -2,7 +2,9 @@ package com.baeldung.hibernate.proxy;
|
|||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
public class Company implements Serializable {
|
||||
|
@ -14,6 +16,10 @@ public class Company implements Serializable {
|
|||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@OneToMany
|
||||
@JoinColumn(name = "workplace_id")
|
||||
private Set<Employee> employees = new HashSet<>();
|
||||
|
||||
public Company() { }
|
||||
|
||||
public Company(String name) {
|
||||
|
@ -36,6 +42,10 @@ public class Company implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<Employee> getEmployees() {
|
||||
return this.employees;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
|
|
@ -15,6 +15,7 @@ public class Employee implements Serializable {
|
|||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "workplace_id")
|
||||
private Company workplace;
|
||||
|
||||
@Column(name = "first_name")
|
||||
|
|
|
@ -50,19 +50,65 @@ public class HibernateProxyUnitTest {
|
|||
}
|
||||
|
||||
@Test(expected = ObjectNotFoundException.class)
|
||||
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException() {
|
||||
public void givenAnNonExistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException() {
|
||||
Employee employee = session.load(Employee.class, 999L);
|
||||
assertNull(employee);
|
||||
employee.getId();
|
||||
assertNotNull(employee);
|
||||
employee.getFirstName();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
|
||||
public void givenAnNonExistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
|
||||
Employee employee = session.load(Employee.class, 14L);
|
||||
assertNotNull(employee);
|
||||
assertTrue(employee instanceof HibernateProxy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnEmployeeFromACompany_whenUseLoadMethod_thenCompanyIsAProxy() {
|
||||
Transaction tx = session.beginTransaction();
|
||||
|
||||
this.workplace = new Company("Bizco");
|
||||
session.save(workplace);
|
||||
|
||||
this.albert = new Employee(workplace, "Albert");
|
||||
session.save(albert);
|
||||
|
||||
session.flush();
|
||||
session.clear();
|
||||
|
||||
tx.commit();
|
||||
this.session = factory.openSession();
|
||||
|
||||
Employee proxyAlbert = session.load(Employee.class, albert.getId());
|
||||
assertTrue(proxyAlbert instanceof HibernateProxy);
|
||||
|
||||
// with many-to-one lazy-loading, associations remain proxies
|
||||
assertTrue(proxyAlbert.getWorkplace() instanceof HibernateProxy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACompanyWithEmployees_whenUseLoadMethod_thenEmployeesAreProxies() {
|
||||
Transaction tx = session.beginTransaction();
|
||||
|
||||
this.workplace = new Company("Bizco");
|
||||
session.save(workplace);
|
||||
|
||||
this.albert = new Employee(workplace, "Albert");
|
||||
session.save(albert);
|
||||
|
||||
session.flush();
|
||||
session.clear();
|
||||
|
||||
tx.commit();
|
||||
this.session = factory.openSession();
|
||||
|
||||
Company proxyBizco = session.load(Company.class, workplace.getId());
|
||||
assertTrue(proxyBizco instanceof HibernateProxy);
|
||||
|
||||
// with one-to-many, associations aren't proxies
|
||||
assertFalse(proxyBizco.getEmployees().iterator().next() instanceof HibernateProxy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery() {
|
||||
Transaction tx = session.beginTransaction();
|
||||
|
|
Loading…
Reference in New Issue