Skip to content

Commit 9a085b0

Browse files
authored
Polish Session.load Tests (eugenp#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
1 parent 52c2ce4 commit 9a085b0

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import javax.persistence.*;
44
import java.io.Serializable;
5+
import java.util.HashSet;
56
import java.util.Objects;
7+
import java.util.Set;
68

79
@Entity
810
public class Company implements Serializable {
@@ -14,6 +16,10 @@ public class Company implements Serializable {
1416
@Column(name = "name")
1517
private String name;
1618

19+
@OneToMany
20+
@JoinColumn(name = "workplace_id")
21+
private Set<Employee> employees = new HashSet<>();
22+
1723
public Company() { }
1824

1925
public Company(String name) {
@@ -36,6 +42,10 @@ public void setName(String name) {
3642
this.name = name;
3743
}
3844

45+
public Set<Employee> getEmployees() {
46+
return this.employees;
47+
}
48+
3949
@Override
4050
public boolean equals(Object o) {
4151
if (this == o) return true;

‎hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Employee implements Serializable {
1515
private Long id;
1616

1717
@ManyToOne(fetch = FetchType.LAZY)
18+
@JoinColumn(name = "workplace_id")
1819
private Company workplace;
1920

2021
@Column(name = "first_name")

‎hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,65 @@ public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
5050
}
5151

5252
@Test(expected = ObjectNotFoundException.class)
53-
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException() {
53+
public void givenAnNonExistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException() {
5454
Employee employee = session.load(Employee.class, 999L);
55-
assertNull(employee);
56-
employee.getId();
55+
assertNotNull(employee);
56+
employee.getFirstName();
5757
}
5858

5959
@Test
60-
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
60+
public void givenAnNonExistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
6161
Employee employee = session.load(Employee.class, 14L);
6262
assertNotNull(employee);
6363
assertTrue(employee instanceof HibernateProxy);
6464
}
6565

66+
@Test
67+
public void givenAnEmployeeFromACompany_whenUseLoadMethod_thenCompanyIsAProxy() {
68+
Transaction tx = session.beginTransaction();
69+
70+
this.workplace = new Company("Bizco");
71+
session.save(workplace);
72+
73+
this.albert = new Employee(workplace, "Albert");
74+
session.save(albert);
75+
76+
session.flush();
77+
session.clear();
78+
79+
tx.commit();
80+
this.session = factory.openSession();
81+
82+
Employee proxyAlbert = session.load(Employee.class, albert.getId());
83+
assertTrue(proxyAlbert instanceof HibernateProxy);
84+
85+
// with many-to-one lazy-loading, associations remain proxies
86+
assertTrue(proxyAlbert.getWorkplace() instanceof HibernateProxy);
87+
}
88+
89+
@Test
90+
public void givenACompanyWithEmployees_whenUseLoadMethod_thenEmployeesAreProxies() {
91+
Transaction tx = session.beginTransaction();
92+
93+
this.workplace = new Company("Bizco");
94+
session.save(workplace);
95+
96+
this.albert = new Employee(workplace, "Albert");
97+
session.save(albert);
98+
99+
session.flush();
100+
session.clear();
101+
102+
tx.commit();
103+
this.session = factory.openSession();
104+
105+
Company proxyBizco = session.load(Company.class, workplace.getId());
106+
assertTrue(proxyBizco instanceof HibernateProxy);
107+
108+
// with one-to-many, associations aren't proxies
109+
assertFalse(proxyBizco.getEmployees().iterator().next() instanceof HibernateProxy);
110+
}
111+
66112
@Test
67113
public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery() {
68114
Transaction tx = session.beginTransaction();

0 commit comments

Comments
 (0)