From 9a085b0e8f1ac6892dca4eff4ca9e6edeb476547 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Tue, 2 Oct 2018 07:22:12 -0600 Subject: [PATCH] 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 --- .../com/baeldung/hibernate/proxy/Company.java | 10 ++++ .../baeldung/hibernate/proxy/Employee.java | 1 + .../proxy/HibernateProxyUnitTest.java | 54 +++++++++++++++++-- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java index b21078dfeb..f146a8674e 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java @@ -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 employees = new HashSet<>(); + public Company() { } public Company(String name) { @@ -36,6 +42,10 @@ public class Company implements Serializable { this.name = name; } + public Set getEmployees() { + return this.employees; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java index 2c2aa7956d..4bc0b8f25c 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java @@ -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") diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java index 242e6b41d7..0a4caf032b 100644 --- a/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java +++ b/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java @@ -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();