From 8a5d9843f6e7ee86a8f7bb142f3dfce5f47d1068 Mon Sep 17 00:00:00 2001 From: Felipe Santiago Corro Date: Mon, 1 Oct 2018 23:42:46 -0300 Subject: [PATCH] Class Refactor (#5374) New tests in Hibernate with proxy --- .../hibernate/proxy/BatchEmployee.java | 56 ------------- .../proxy/{Boss.java => Company.java} | 25 +++--- .../baeldung/hibernate/proxy/Employee.java | 49 +++++++---- .../hibernate/proxy/HibernateUtil.java | 2 +- .../proxy/HibernateProxyUnitTest.java | 81 +++++++++++++------ 5 files changed, 105 insertions(+), 108 deletions(-) delete mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java rename hibernate5/src/main/java/com/baeldung/hibernate/proxy/{Boss.java => Company.java} (51%) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java deleted file mode 100644 index 00643ab3dd..0000000000 --- a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java +++ /dev/null @@ -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; - } -} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java similarity index 51% rename from hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java rename to hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java index b6e01814d0..b21078dfeb 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Company.java @@ -2,9 +2,10 @@ package com.baeldung.hibernate.proxy; import javax.persistence.*; import java.io.Serializable; +import java.util.Objects; @Entity -public class Boss implements Serializable { +public class Company implements Serializable { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) @@ -13,14 +14,10 @@ public class Boss implements Serializable { @Column(name = "name") private String name; - @Column(name = "surname") - private String surname; + public Company() { } - public Boss() { } - - public Boss(String name, String surname) { + public Company(String name) { this.name = name; - this.surname = surname; } public Long getId() { @@ -39,11 +36,17 @@ public class Boss implements Serializable { this.name = name; } - public String getSurname() { - return surname; + @Override + 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) { - this.surname = surname; + @Override + public int hashCode() { + return Objects.hash(id, name); } } diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java index 6bc64c35ef..2c2aa7956d 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java @@ -1,9 +1,13 @@ package com.baeldung.hibernate.proxy; +import org.hibernate.annotations.BatchSize; + import javax.persistence.*; import java.io.Serializable; +import java.util.Objects; @Entity +@BatchSize(size = 5) public class Employee implements Serializable { @Id @@ -11,13 +15,17 @@ public class Employee implements Serializable { private Long id; @ManyToOne(fetch = FetchType.LAZY) - private Boss boss; + private Company workplace; - @Column(name = "name") - private String name; + @Column(name = "first_name") + private String firstName; - @Column(name = "surname") - private String surname; + public Employee() { } + + public Employee(Company workplace, String firstName) { + this.workplace = workplace; + this.firstName = firstName; + } public Long getId() { return id; @@ -27,27 +35,34 @@ public class Employee implements Serializable { this.id = id; } - public Boss getBoss() { - return boss; + public Company getWorkplace() { + return workplace; } - public void setBoss(Boss boss) { - this.boss = boss; + public void setWorkplace(Company workplace) { + this.workplace = workplace; } - public String getName() { - return name; + public String getFirstName() { + return firstName; } - public void setName(String name) { - this.name = name; + public void setFirstName(String firstName) { + this.firstName = firstName; } - public String getSurname() { - return surname; + @Override + 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) { - this.surname = surname; + @Override + public int hashCode() { + return Objects.hash(id, workplace, firstName); } } diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java index e6ad0432bd..37c083049f 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java @@ -30,7 +30,7 @@ public class HibernateUtil { private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) { MetadataSources metadataSources = new MetadataSources(serviceRegistry); metadataSources.addPackage("com.baeldung.hibernate.proxy"); - metadataSources.addAnnotatedClass(Boss.class); + metadataSources.addAnnotatedClass(Company.class); metadataSources.addAnnotatedClass(Employee.class); Metadata metadata = metadataSources.buildMetadata(); diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java index fa41797dd2..242e6b41d7 100644 --- a/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java +++ b/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.hibernate.proxy; import org.hibernate.*; +import org.hibernate.proxy.HibernateProxy; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -8,25 +9,31 @@ 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 SessionFactory factory; + private Session session; + private Company workplace; + + private Employee albert; + + private Employee bob; + + private Employee charlotte; + @Before public void init(){ try { - session = HibernateUtil.getSessionFactory("hibernate.properties") - .openSession(); + factory = HibernateUtil.getSessionFactory("hibernate.properties"); + session = factory.openSession(); } catch (HibernateException | IOException e) { fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]"); } - - Boss boss = new Boss("Eduard", "Freud"); - session.save(boss); } @After @@ -36,40 +43,68 @@ public class HibernateProxyUnitTest { } } - @Test(expected = NullPointerException.class) + @Test 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); employee.getId(); } @Test public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() { - Employee employee = session.load(Employee.class, new Long(14)); + Employee employee = session.load(Employee.class, 14L); assertNotNull(employee); + assertTrue(employee instanceof HibernateProxy); } @Test - public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() { - Transaction transaction = session.beginTransaction(); + public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery() { + Transaction tx = session.beginTransaction(); - for (long i = 1; i <= 5; i++) { - Employee employee = new Employee(); - employee.setName("Employee " + i); - session.save(employee); - } + //We are saving 3 entities with one flush + + this.workplace = new Company("Bizco"); + 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.clear(); - transaction.commit(); - transaction = session.beginTransaction(); + tx.commit(); + session = factory.openSession(); - List employeeList = session.createQuery("from Employee") - .setCacheMode(CacheMode.IGNORE).getResultList(); + Employee proxyAlbert = session.load(Employee.class, this.albert.getId()); + assertNotNull(proxyAlbert); + assertTrue(proxyAlbert instanceof HibernateProxy); - assertEquals(employeeList.size(), 5); - transaction.commit(); + Employee proxyBob = session.load(Employee.class, this.bob.getId()); + 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); } }