Skip to content

Commit 8a5d984

Browse files
fscorromaibin
authored andcommitted
Class Refactor (eugenp#5374)
New tests in Hibernate with proxy
1 parent ea22559 commit 8a5d984

5 files changed

Lines changed: 105 additions & 108 deletions

File tree

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

Lines changed: 0 additions & 56 deletions
This file was deleted.

hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java renamed to hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java

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

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

67
@Entity
7-
public class Boss implements Serializable {
8+
public class Company implements Serializable {
89

910
@Id
1011
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@@ -13,14 +14,10 @@ public class Boss implements Serializable {
1314
@Column(name = "name")
1415
private String name;
1516

16-
@Column(name = "surname")
17-
private String surname;
17+
public Company() { }
1818

19-
public Boss() { }
20-
21-
public Boss(String name, String surname) {
19+
public Company(String name) {
2220
this.name = name;
23-
this.surname = surname;
2421
}
2522

2623
public Long getId() {
@@ -39,11 +36,17 @@ public void setName(String name) {
3936
this.name = name;
4037
}
4138

42-
public String getSurname() {
43-
return surname;
39+
@Override
40+
public boolean equals(Object o) {
41+
if (this == o) return true;
42+
if (o == null || getClass() != o.getClass()) return false;
43+
Company company = (Company) o;
44+
return Objects.equals(id, company.id) &&
45+
Objects.equals(name, company.name);
4446
}
4547

46-
public void setSurname(String surname) {
47-
this.surname = surname;
48+
@Override
49+
public int hashCode() {
50+
return Objects.hash(id, name);
4851
}
4952
}
Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package com.baeldung.hibernate.proxy;
22

3+
import org.hibernate.annotations.BatchSize;
4+
35
import javax.persistence.*;
46
import java.io.Serializable;
7+
import java.util.Objects;
58

69
@Entity
10+
@BatchSize(size = 5)
711
public class Employee implements Serializable {
812

913
@Id
1014
@GeneratedValue (strategy = GenerationType.SEQUENCE)
1115
private Long id;
1216

1317
@ManyToOne(fetch = FetchType.LAZY)
14-
private Boss boss;
18+
private Company workplace;
19+
20+
@Column(name = "first_name")
21+
private String firstName;
1522

16-
@Column(name = "name")
17-
private String name;
23+
public Employee() { }
1824

19-
@Column(name = "surname")
20-
private String surname;
25+
public Employee(Company workplace, String firstName) {
26+
this.workplace = workplace;
27+
this.firstName = firstName;
28+
}
2129

2230
public Long getId() {
2331
return id;
@@ -27,27 +35,34 @@ public void setId(Long id) {
2735
this.id = id;
2836
}
2937

30-
public Boss getBoss() {
31-
return boss;
38+
public Company getWorkplace() {
39+
return workplace;
3240
}
3341

34-
public void setBoss(Boss boss) {
35-
this.boss = boss;
42+
public void setWorkplace(Company workplace) {
43+
this.workplace = workplace;
3644
}
3745

38-
public String getName() {
39-
return name;
46+
public String getFirstName() {
47+
return firstName;
4048
}
4149

42-
public void setName(String name) {
43-
this.name = name;
50+
public void setFirstName(String firstName) {
51+
this.firstName = firstName;
4452
}
4553

46-
public String getSurname() {
47-
return surname;
54+
@Override
55+
public boolean equals(Object o) {
56+
if (this == o) return true;
57+
if (o == null || getClass() != o.getClass()) return false;
58+
Employee employee = (Employee) o;
59+
return Objects.equals(id, employee.id) &&
60+
Objects.equals(workplace, employee.workplace) &&
61+
Objects.equals(firstName, employee.firstName);
4862
}
4963

50-
public void setSurname(String surname) {
51-
this.surname = surname;
64+
@Override
65+
public int hashCode() {
66+
return Objects.hash(id, workplace, firstName);
5267
}
5368
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static SessionFactory getSessionFactory(String propertyFileName) throws I
3030
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
3131
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
3232
metadataSources.addPackage("com.baeldung.hibernate.proxy");
33-
metadataSources.addAnnotatedClass(Boss.class);
33+
metadataSources.addAnnotatedClass(Company.class);
3434
metadataSources.addAnnotatedClass(Employee.class);
3535

3636
Metadata metadata = metadataSources.buildMetadata();
Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
package com.baeldung.hibernate.proxy;
22

33
import org.hibernate.*;
4+
import org.hibernate.proxy.HibernateProxy;
45
import org.junit.After;
56
import org.junit.Before;
67
import org.junit.Test;
78

89
import static org.junit.Assert.*;
910

1011
import java.io.IOException;
11-
import java.util.List;
1212

1313
import static org.junit.Assert.fail;
1414

1515
public class HibernateProxyUnitTest {
1616

17+
private SessionFactory factory;
18+
1719
private Session session;
1820

21+
private Company workplace;
22+
23+
private Employee albert;
24+
25+
private Employee bob;
26+
27+
private Employee charlotte;
28+
1929
@Before
2030
public void init(){
2131
try {
22-
session = HibernateUtil.getSessionFactory("hibernate.properties")
23-
.openSession();
32+
factory = HibernateUtil.getSessionFactory("hibernate.properties");
33+
session = factory.openSession();
2434
} catch (HibernateException | IOException e) {
2535
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
2636
}
27-
28-
Boss boss = new Boss("Eduard", "Freud");
29-
session.save(boss);
3037
}
3138

3239
@After
@@ -36,40 +43,68 @@ public void close(){
3643
}
3744
}
3845

39-
@Test(expected = NullPointerException.class)
46+
@Test
4047
public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
41-
Employee employee = session.get(Employee.class, new Long(14));
48+
Employee employee = session.get(Employee.class, 14L);
49+
assertNull(employee);
50+
}
51+
52+
@Test(expected = ObjectNotFoundException.class)
53+
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException() {
54+
Employee employee = session.load(Employee.class, 999L);
4255
assertNull(employee);
4356
employee.getId();
4457
}
4558

4659
@Test
4760
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
48-
Employee employee = session.load(Employee.class, new Long(14));
61+
Employee employee = session.load(Employee.class, 14L);
4962
assertNotNull(employee);
63+
assertTrue(employee instanceof HibernateProxy);
5064
}
5165

5266
@Test
53-
public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() {
54-
Transaction transaction = session.beginTransaction();
67+
public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery() {
68+
Transaction tx = session.beginTransaction();
5569

56-
for (long i = 1; i <= 5; i++) {
57-
Employee employee = new Employee();
58-
employee.setName("Employee " + i);
59-
session.save(employee);
60-
}
70+
//We are saving 3 entities with one flush
71+
72+
this.workplace = new Company("Bizco");
73+
session.save(workplace);
74+
75+
this.albert = new Employee(workplace, "Albert");
76+
session.save(albert);
77+
78+
this.bob = new Employee(workplace, "Bob");
79+
session.save(bob);
80+
81+
this.charlotte = new Employee(workplace, "Charlotte");
82+
session.save(charlotte);
6183

62-
//After this line is possible to see all the insertions in the logs
6384
session.flush();
6485
session.clear();
65-
transaction.commit();
6686

67-
transaction = session.beginTransaction();
87+
tx.commit();
88+
session = factory.openSession();
89+
90+
Employee proxyAlbert = session.load(Employee.class, this.albert.getId());
91+
assertNotNull(proxyAlbert);
92+
assertTrue(proxyAlbert instanceof HibernateProxy);
93+
94+
Employee proxyBob = session.load(Employee.class, this.bob.getId());
95+
assertNotNull(proxyBob);
96+
assertTrue(proxyBob instanceof HibernateProxy);
97+
98+
Employee proxyCharlotte = session.load(Employee.class, this.charlotte.getId());
99+
assertNotNull(proxyCharlotte);
100+
assertTrue(proxyCharlotte instanceof HibernateProxy);
68101

69-
List<Employee> employeeList = session.createQuery("from Employee")
70-
.setCacheMode(CacheMode.IGNORE).getResultList();
102+
//Fetching from database 3 entities with one call
103+
//Select from log: where employee0_.id in (?, ?, ?)
104+
proxyAlbert.getFirstName();
71105

72-
assertEquals(employeeList.size(), 5);
73-
transaction.commit();
106+
assertEquals(proxyAlbert, this.albert);
107+
assertEquals(proxyBob, this.bob);
108+
assertEquals(proxyCharlotte, this.charlotte);
74109
}
75110
}

0 commit comments

Comments
 (0)