Proxy in Hibernate load() method (#5222)

This commit is contained in:
Felipe Santiago Corro 2018-09-10 18:01:47 -03:00 committed by maibin
parent 6f2674816e
commit 50f0d57182
5 changed files with 290 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package com.baeldung.hibernate.proxy;
import org.hibernate.annotations.BatchSize;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@BatchSize(size = 5)
public class BatchEmployee implements Serializable {
@Id
@GeneratedValue (strategy = GenerationType.SEQUENCE)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Boss boss;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.hibernate.proxy;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Boss implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Boss() { }
public Boss(String name, String surname) {
this.name = name;
this.surname = surname;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.hibernate.proxy;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Employee implements Serializable {
@Id
@GeneratedValue (strategy = GenerationType.SEQUENCE)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Boss boss;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.hibernate.proxy;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static String PROPERTY_FILE_NAME;
public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
PROPERTY_FILE_NAME = propertyFileName;
if (sessionFactory == null) {
ServiceRegistry serviceRegistry = configureServiceRegistry();
sessionFactory = getSessionFactoryBuilder(serviceRegistry).build();
}
return sessionFactory;
}
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
metadataSources.addPackage("com.baeldung.hibernate.proxy");
metadataSources.addAnnotatedClass(Boss.class);
metadataSources.addAnnotatedClass(Employee.class);
Metadata metadata = metadataSources.buildMetadata();
return metadata.getSessionFactoryBuilder();
}
private static ServiceRegistry configureServiceRegistry() throws IOException {
Properties properties = getProperties();
return new StandardServiceRegistryBuilder().applySettings(properties)
.build();
}
private static Properties getProperties() throws IOException {
Properties properties = new Properties();
URL propertiesURL = Thread.currentThread()
.getContextClassLoader()
.getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
properties.load(inputStream);
}
return properties;
}
}

View File

@ -0,0 +1,75 @@
package com.baeldung.hibernate.proxy;
import org.hibernate.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.fail;
public class HibernateProxyUnitTest {
private Session session;
@Before
public void init(){
try {
session = HibernateUtil.getSessionFactory("hibernate.properties")
.openSession();
} catch (HibernateException | IOException e) {
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
}
Boss boss = new Boss("Eduard", "Freud");
session.save(boss);
}
@After
public void close(){
if(session != null) {
session.close();
}
}
@Test(expected = NullPointerException.class)
public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
Employee employee = session.get(Employee.class, new Long(14));
assertNull(employee);
employee.getId();
}
@Test
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
Employee employee = session.load(Employee.class, new Long(14));
assertNotNull(employee);
}
@Test
public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() {
Transaction transaction = session.beginTransaction();
for (long i = 1; i <= 5; i++) {
Employee employee = new Employee();
employee.setName("Employee " + i);
session.save(employee);
}
//After this line is possible to see all the insertions in the logs
session.flush();
session.clear();
transaction.commit();
transaction = session.beginTransaction();
List<Employee> employeeList = session.createQuery("from Employee")
.setCacheMode(CacheMode.IGNORE).getResultList();
assertEquals(employeeList.size(), 5);
transaction.commit();
}
}