Code related to the mini-article BAEL-3386

This commit is contained in:
zepfred 2019-12-29 10:48:41 -03:00
parent 1c3f2d68dd
commit 195ab64929
5 changed files with 253 additions and 0 deletions

View File

@ -0,0 +1,11 @@
package com.baeldung.elementcollection;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ElementCollectionApplication {
public static void main(String[] args) {
SpringApplication.run(ElementCollectionApplication.class, args);
}
}

View File

@ -0,0 +1,71 @@
package com.baeldung.elementcollection.model;
import javax.persistence.*;
import java.util.List;
import java.util.Objects;
@Entity
public class Employee {
@Id
private int id;
private String name;
@ElementCollection
@CollectionTable(
name = "employee_phone",
joinColumns = @JoinColumn(name = "employee_id")
)
private List<Phone> phones;
public Employee() {
}
public Employee(int id) {
this.id = id;
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Phone> getPhones() {
return phones;
}
public void setPhones(List<Phone> phones) {
this.phones = phones;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Employee)) {
return false;
}
Employee user = (Employee) o;
return getId() == user.getId();
}
@Override
public int hashCode() {
return Objects.hash(getId());
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.elementcollection.model;
import javax.persistence.Embeddable;
import java.util.Objects;
@Embeddable
public class Phone {
private String type;
private String areaCode;
private String number;
public Phone() {
}
public Phone(String type, String areaCode, String number) {
this.type = type;
this.areaCode = areaCode;
this.number = number;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAreaCode() {
return areaCode;
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Phone)) {
return false;
}
Phone phone = (Phone) o;
return getType().equals(phone.getType()) &&
getAreaCode().equals(phone.getAreaCode()) &&
getNumber().equals(phone.getNumber());
}
@Override
public int hashCode() {
return Objects.hash(getType(), getAreaCode(), getNumber());
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.elementcollection.repository;
import com.baeldung.elementcollection.model.Employee;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeRepository {
@PersistenceContext
private EntityManager em;
@Transactional
public void save(Employee employee) {
em.persist(employee);
}
@Transactional
public void remove(int id) {
Employee employee = findById(id);
em.remove(employee);
}
public Employee findById(int id) {
return em.find(Employee.class, id);
}
public Employee findByJPQL(int id) {
return em.createQuery("SELECT u FROM Employee AS u JOIN FETCH u.phones WHERE u.id=:id", Employee.class).setParameter("id", id).getSingleResult();
}
public Employee findByEntityGraph(int id) {
EntityGraph<Employee> entityGraph = em.createEntityGraph(Employee.class);
entityGraph.addAttributeNodes("name", "phones");
Map<String, Object> properties = new HashMap<>();
properties.put("javax.persistence.fetchgraph", entityGraph);
return em.find(Employee.class, id, properties);
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.elementcollection;
import com.baeldung.elementcollection.model.Employee;
import com.baeldung.elementcollection.model.Phone;
import com.baeldung.elementcollection.repository.EmployeeRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ElementCollectionApplication.class)
public class ElementCollectionIntegrationTest {
@Autowired
private EmployeeRepository employeeRepository;
@Before
public void init() {
Employee employee = new Employee(1, "Fred");
employee.setPhones(Arrays.asList(new Phone("work", "+55", "99999-9999"), new Phone("home", "+55", "98888-8888")));
employeeRepository.save(employee);
}
@After
public void clean() {
employeeRepository.remove(1);
}
@Test(expected = org.hibernate.LazyInitializationException.class)
public void whenAccessLazyCollection_thenThrowLazyInitializationException() {
Employee employee = employeeRepository.findById(1);
assertThat(employee.getPhones().size(), is(2));
}
@Test
public void whenUseJPAQL_thenFetchResult() {
Employee employee = employeeRepository.findByJPQL(1);
assertThat(employee.getPhones().size(), is(2));
}
@Test
public void whenUseEntityGraph_thenFetchResult() {
Employee employee = employeeRepository.findByEntityGraph(1);
assertThat(employee.getPhones().size(), is(2));
}
@Test
@Transactional
public void whenUseTransaction_thenFetchResult() {
Employee employee = employeeRepository.findById(1);
assertThat(employee.getPhones().size(), is(2));
}
}