Merge branch 'master' into thombergs-patch-28

This commit is contained in:
Tom Hombergs
2018-10-02 20:39:29 +02:00
committed by GitHub
547 changed files with 11329 additions and 2641 deletions

View File

@@ -15,3 +15,4 @@
- [Optimistic Locking in JPA](http://www.baeldung.com/jpa-optimistic-locking)
- [Hibernate Entity Lifecycle](https://www.baeldung.com/hibernate-entity-lifecycle)
- [Mapping A Hibernate Query to a Custom Class](https://www.baeldung.com/hibernate-query-to-custom-class)
- [@JoinColumn Annotation Explained](https://www.baeldung.com/jpa-join-column)

View File

@@ -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;
}
}

View File

@@ -1,49 +0,0 @@
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,62 @@
package com.baeldung.hibernate.proxy;
import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@Entity
public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(name = "name")
private String name;
@OneToMany
@JoinColumn(name = "workplace_id")
private Set<Employee> employees = new HashSet<>();
public Company() { }
public Company(String name) {
this.name = name;
}
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 Set<Employee> getEmployees() {
return this.employees;
}
@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);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}

View File

@@ -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,18 @@ public class Employee implements Serializable {
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Boss boss;
@JoinColumn(name = "workplace_id")
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 +36,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);
}
}

View File

@@ -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();

View File

@@ -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,114 @@ 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);
employee.getId();
}
@Test
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
Employee employee = session.load(Employee.class, new Long(14));
@Test(expected = ObjectNotFoundException.class)
public void givenAnNonExistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException() {
Employee employee = session.load(Employee.class, 999L);
assertNotNull(employee);
employee.getFirstName();
}
@Test
public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() {
Transaction transaction = session.beginTransaction();
public void givenAnNonExistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
Employee employee = session.load(Employee.class, 14L);
assertNotNull(employee);
assertTrue(employee instanceof HibernateProxy);
}
for (long i = 1; i <= 5; i++) {
Employee employee = new Employee();
employee.setName("Employee " + i);
session.save(employee);
}
@Test
public void givenAnEmployeeFromACompany_whenUseLoadMethod_thenCompanyIsAProxy() {
Transaction tx = session.beginTransaction();
this.workplace = new Company("Bizco");
session.save(workplace);
this.albert = new Employee(workplace, "Albert");
session.save(albert);
//After this line is possible to see all the insertions in the logs
session.flush();
session.clear();
transaction.commit();
transaction = session.beginTransaction();
tx.commit();
this.session = factory.openSession();
List<Employee> employeeList = session.createQuery("from Employee")
.setCacheMode(CacheMode.IGNORE).getResultList();
Employee proxyAlbert = session.load(Employee.class, albert.getId());
assertTrue(proxyAlbert instanceof HibernateProxy);
assertEquals(employeeList.size(), 5);
transaction.commit();
// with many-to-one lazy-loading, associations remain proxies
assertTrue(proxyAlbert.getWorkplace() instanceof HibernateProxy);
}
@Test
public void givenACompanyWithEmployees_whenUseLoadMethod_thenEmployeesAreProxies() {
Transaction tx = session.beginTransaction();
this.workplace = new Company("Bizco");
session.save(workplace);
this.albert = new Employee(workplace, "Albert");
session.save(albert);
session.flush();
session.clear();
tx.commit();
this.session = factory.openSession();
Company proxyBizco = session.load(Company.class, workplace.getId());
assertTrue(proxyBizco instanceof HibernateProxy);
// with one-to-many, associations aren't proxies
assertFalse(proxyBizco.getEmployees().iterator().next() instanceof HibernateProxy);
}
@Test
public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery() {
Transaction tx = session.beginTransaction();
//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);
session.flush();
session.clear();
tx.commit();
session = factory.openSession();
Employee proxyAlbert = session.load(Employee.class, this.albert.getId());
assertNotNull(proxyAlbert);
assertTrue(proxyAlbert instanceof HibernateProxy);
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);
}
}