Skip to content

Commit 50f0d57

Browse files
fscorromaibin
authored andcommitted
Proxy in Hibernate load() method (eugenp#5222)
1 parent 6f26748 commit 50f0d57

5 files changed

Lines changed: 290 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.hibernate.proxy;
2+
3+
import org.hibernate.annotations.BatchSize;
4+
5+
import javax.persistence.*;
6+
import java.io.Serializable;
7+
8+
@Entity
9+
@BatchSize(size = 5)
10+
public class BatchEmployee implements Serializable {
11+
12+
@Id
13+
@GeneratedValue (strategy = GenerationType.SEQUENCE)
14+
private Long id;
15+
16+
@ManyToOne(fetch = FetchType.LAZY)
17+
private Boss boss;
18+
19+
@Column(name = "name")
20+
private String name;
21+
22+
@Column(name = "surname")
23+
private String surname;
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public Boss getBoss() {
34+
return boss;
35+
}
36+
37+
public void setBoss(Boss boss) {
38+
this.boss = boss;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
public String getSurname() {
50+
return surname;
51+
}
52+
53+
public void setSurname(String surname) {
54+
this.surname = surname;
55+
}
56+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.hibernate.proxy;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
6+
@Entity
7+
public class Boss implements Serializable {
8+
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
11+
private Long id;
12+
13+
@Column(name = "name")
14+
private String name;
15+
16+
@Column(name = "surname")
17+
private String surname;
18+
19+
public Boss() { }
20+
21+
public Boss(String name, String surname) {
22+
this.name = name;
23+
this.surname = surname;
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public String getSurname() {
43+
return surname;
44+
}
45+
46+
public void setSurname(String surname) {
47+
this.surname = surname;
48+
}
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.hibernate.proxy;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
6+
@Entity
7+
public class Employee implements Serializable {
8+
9+
@Id
10+
@GeneratedValue (strategy = GenerationType.SEQUENCE)
11+
private Long id;
12+
13+
@ManyToOne(fetch = FetchType.LAZY)
14+
private Boss boss;
15+
16+
@Column(name = "name")
17+
private String name;
18+
19+
@Column(name = "surname")
20+
private String surname;
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public Boss getBoss() {
31+
return boss;
32+
}
33+
34+
public void setBoss(Boss boss) {
35+
this.boss = boss;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
public String getSurname() {
47+
return surname;
48+
}
49+
50+
public void setSurname(String surname) {
51+
this.surname = surname;
52+
}
53+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.hibernate.proxy;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.hibernate.SessionFactory;
5+
import org.hibernate.boot.Metadata;
6+
import org.hibernate.boot.MetadataSources;
7+
import org.hibernate.boot.SessionFactoryBuilder;
8+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
9+
import org.hibernate.service.ServiceRegistry;
10+
11+
import java.io.FileInputStream;
12+
import java.io.IOException;
13+
import java.net.URL;
14+
import java.util.Properties;
15+
16+
public class HibernateUtil {
17+
18+
private static SessionFactory sessionFactory;
19+
private static String PROPERTY_FILE_NAME;
20+
21+
public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
22+
PROPERTY_FILE_NAME = propertyFileName;
23+
if (sessionFactory == null) {
24+
ServiceRegistry serviceRegistry = configureServiceRegistry();
25+
sessionFactory = getSessionFactoryBuilder(serviceRegistry).build();
26+
}
27+
return sessionFactory;
28+
}
29+
30+
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
31+
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
32+
metadataSources.addPackage("com.baeldung.hibernate.proxy");
33+
metadataSources.addAnnotatedClass(Boss.class);
34+
metadataSources.addAnnotatedClass(Employee.class);
35+
36+
Metadata metadata = metadataSources.buildMetadata();
37+
return metadata.getSessionFactoryBuilder();
38+
39+
}
40+
41+
private static ServiceRegistry configureServiceRegistry() throws IOException {
42+
Properties properties = getProperties();
43+
return new StandardServiceRegistryBuilder().applySettings(properties)
44+
.build();
45+
}
46+
47+
private static Properties getProperties() throws IOException {
48+
Properties properties = new Properties();
49+
URL propertiesURL = Thread.currentThread()
50+
.getContextClassLoader()
51+
.getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
52+
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
53+
properties.load(inputStream);
54+
}
55+
return properties;
56+
}
57+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.hibernate.proxy;
2+
3+
import org.hibernate.*;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.*;
9+
10+
import java.io.IOException;
11+
import java.util.List;
12+
13+
import static org.junit.Assert.fail;
14+
15+
public class HibernateProxyUnitTest {
16+
17+
private Session session;
18+
19+
@Before
20+
public void init(){
21+
try {
22+
session = HibernateUtil.getSessionFactory("hibernate.properties")
23+
.openSession();
24+
} catch (HibernateException | IOException e) {
25+
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
26+
}
27+
28+
Boss boss = new Boss("Eduard", "Freud");
29+
session.save(boss);
30+
}
31+
32+
@After
33+
public void close(){
34+
if(session != null) {
35+
session.close();
36+
}
37+
}
38+
39+
@Test(expected = NullPointerException.class)
40+
public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
41+
Employee employee = session.get(Employee.class, new Long(14));
42+
assertNull(employee);
43+
employee.getId();
44+
}
45+
46+
@Test
47+
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
48+
Employee employee = session.load(Employee.class, new Long(14));
49+
assertNotNull(employee);
50+
}
51+
52+
@Test
53+
public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() {
54+
Transaction transaction = session.beginTransaction();
55+
56+
for (long i = 1; i <= 5; i++) {
57+
Employee employee = new Employee();
58+
employee.setName("Employee " + i);
59+
session.save(employee);
60+
}
61+
62+
//After this line is possible to see all the insertions in the logs
63+
session.flush();
64+
session.clear();
65+
transaction.commit();
66+
67+
transaction = session.beginTransaction();
68+
69+
List<Employee> employeeList = session.createQuery("from Employee")
70+
.setCacheMode(CacheMode.IGNORE).getResultList();
71+
72+
assertEquals(employeeList.size(), 5);
73+
transaction.commit();
74+
}
75+
}

0 commit comments

Comments
 (0)