parent
ea22559064
commit
8a5d9843f6
@ -1,56 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,9 +2,10 @@ package com.baeldung.hibernate.proxy;
|
|||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Boss implements Serializable {
|
public class Company implements Serializable {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||||
@ -13,14 +14,10 @@ public class Boss implements Serializable {
|
|||||||
@Column(name = "name")
|
@Column(name = "name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column(name = "surname")
|
public Company() { }
|
||||||
private String surname;
|
|
||||||
|
|
||||||
public Boss() { }
|
public Company(String name) {
|
||||||
|
|
||||||
public Boss(String name, String surname) {
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.surname = surname;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
@ -39,11 +36,17 @@ public class Boss implements Serializable {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSurname() {
|
@Override
|
||||||
return surname;
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Company company = (Company) o;
|
||||||
|
return Objects.equals(id, company.id) &&
|
||||||
|
Objects.equals(name, company.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSurname(String surname) {
|
@Override
|
||||||
this.surname = surname;
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,13 @@
|
|||||||
package com.baeldung.hibernate.proxy;
|
package com.baeldung.hibernate.proxy;
|
||||||
|
|
||||||
|
import org.hibernate.annotations.BatchSize;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@BatchSize(size = 5)
|
||||||
public class Employee implements Serializable {
|
public class Employee implements Serializable {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@ -11,13 +15,17 @@ public class Employee implements Serializable {
|
|||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
private Boss boss;
|
private Company workplace;
|
||||||
|
|
||||||
@Column(name = "name")
|
@Column(name = "first_name")
|
||||||
private String name;
|
private String firstName;
|
||||||
|
|
||||||
@Column(name = "surname")
|
public Employee() { }
|
||||||
private String surname;
|
|
||||||
|
public Employee(Company workplace, String firstName) {
|
||||||
|
this.workplace = workplace;
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
@ -27,27 +35,34 @@ public class Employee implements Serializable {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boss getBoss() {
|
public Company getWorkplace() {
|
||||||
return boss;
|
return workplace;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBoss(Boss boss) {
|
public void setWorkplace(Company workplace) {
|
||||||
this.boss = boss;
|
this.workplace = workplace;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getFirstName() {
|
||||||
return name;
|
return firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setFirstName(String firstName) {
|
||||||
this.name = name;
|
this.firstName = firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSurname() {
|
@Override
|
||||||
return surname;
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Employee employee = (Employee) o;
|
||||||
|
return Objects.equals(id, employee.id) &&
|
||||||
|
Objects.equals(workplace, employee.workplace) &&
|
||||||
|
Objects.equals(firstName, employee.firstName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSurname(String surname) {
|
@Override
|
||||||
this.surname = surname;
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, workplace, firstName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ public class HibernateUtil {
|
|||||||
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
|
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
|
||||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||||
metadataSources.addPackage("com.baeldung.hibernate.proxy");
|
metadataSources.addPackage("com.baeldung.hibernate.proxy");
|
||||||
metadataSources.addAnnotatedClass(Boss.class);
|
metadataSources.addAnnotatedClass(Company.class);
|
||||||
metadataSources.addAnnotatedClass(Employee.class);
|
metadataSources.addAnnotatedClass(Employee.class);
|
||||||
|
|
||||||
Metadata metadata = metadataSources.buildMetadata();
|
Metadata metadata = metadataSources.buildMetadata();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.baeldung.hibernate.proxy;
|
package com.baeldung.hibernate.proxy;
|
||||||
|
|
||||||
import org.hibernate.*;
|
import org.hibernate.*;
|
||||||
|
import org.hibernate.proxy.HibernateProxy;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -8,25 +9,31 @@ import org.junit.Test;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
public class HibernateProxyUnitTest {
|
public class HibernateProxyUnitTest {
|
||||||
|
|
||||||
|
private SessionFactory factory;
|
||||||
|
|
||||||
private Session session;
|
private Session session;
|
||||||
|
|
||||||
|
private Company workplace;
|
||||||
|
|
||||||
|
private Employee albert;
|
||||||
|
|
||||||
|
private Employee bob;
|
||||||
|
|
||||||
|
private Employee charlotte;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init(){
|
public void init(){
|
||||||
try {
|
try {
|
||||||
session = HibernateUtil.getSessionFactory("hibernate.properties")
|
factory = HibernateUtil.getSessionFactory("hibernate.properties");
|
||||||
.openSession();
|
session = factory.openSession();
|
||||||
} catch (HibernateException | IOException e) {
|
} catch (HibernateException | IOException e) {
|
||||||
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
|
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
Boss boss = new Boss("Eduard", "Freud");
|
|
||||||
session.save(boss);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@ -36,40 +43,68 @@ public class HibernateProxyUnitTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test
|
||||||
public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
|
public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
|
||||||
Employee employee = session.get(Employee.class, new Long(14));
|
Employee employee = session.get(Employee.class, 14L);
|
||||||
|
assertNull(employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ObjectNotFoundException.class)
|
||||||
|
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException() {
|
||||||
|
Employee employee = session.load(Employee.class, 999L);
|
||||||
assertNull(employee);
|
assertNull(employee);
|
||||||
employee.getId();
|
employee.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
|
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
|
||||||
Employee employee = session.load(Employee.class, new Long(14));
|
Employee employee = session.load(Employee.class, 14L);
|
||||||
assertNotNull(employee);
|
assertNotNull(employee);
|
||||||
|
assertTrue(employee instanceof HibernateProxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() {
|
public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery() {
|
||||||
Transaction transaction = session.beginTransaction();
|
Transaction tx = session.beginTransaction();
|
||||||
|
|
||||||
for (long i = 1; i <= 5; i++) {
|
//We are saving 3 entities with one flush
|
||||||
Employee employee = new Employee();
|
|
||||||
employee.setName("Employee " + i);
|
this.workplace = new Company("Bizco");
|
||||||
session.save(employee);
|
session.save(workplace);
|
||||||
}
|
|
||||||
|
this.albert = new Employee(workplace, "Albert");
|
||||||
|
session.save(albert);
|
||||||
|
|
||||||
|
this.bob = new Employee(workplace, "Bob");
|
||||||
|
session.save(bob);
|
||||||
|
|
||||||
|
this.charlotte = new Employee(workplace, "Charlotte");
|
||||||
|
session.save(charlotte);
|
||||||
|
|
||||||
//After this line is possible to see all the insertions in the logs
|
|
||||||
session.flush();
|
session.flush();
|
||||||
session.clear();
|
session.clear();
|
||||||
transaction.commit();
|
|
||||||
|
|
||||||
transaction = session.beginTransaction();
|
tx.commit();
|
||||||
|
session = factory.openSession();
|
||||||
|
|
||||||
List<Employee> employeeList = session.createQuery("from Employee")
|
Employee proxyAlbert = session.load(Employee.class, this.albert.getId());
|
||||||
.setCacheMode(CacheMode.IGNORE).getResultList();
|
assertNotNull(proxyAlbert);
|
||||||
|
assertTrue(proxyAlbert instanceof HibernateProxy);
|
||||||
|
|
||||||
assertEquals(employeeList.size(), 5);
|
Employee proxyBob = session.load(Employee.class, this.bob.getId());
|
||||||
transaction.commit();
|
assertNotNull(proxyBob);
|
||||||
|
assertTrue(proxyBob instanceof HibernateProxy);
|
||||||
|
|
||||||
|
Employee proxyCharlotte = session.load(Employee.class, this.charlotte.getId());
|
||||||
|
assertNotNull(proxyCharlotte);
|
||||||
|
assertTrue(proxyCharlotte instanceof HibernateProxy);
|
||||||
|
|
||||||
|
//Fetching from database 3 entities with one call
|
||||||
|
//Select from log: where employee0_.id in (?, ?, ?)
|
||||||
|
proxyAlbert.getFirstName();
|
||||||
|
|
||||||
|
assertEquals(proxyAlbert, this.albert);
|
||||||
|
assertEquals(proxyBob, this.bob);
|
||||||
|
assertEquals(proxyCharlotte, this.charlotte);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user