BAEL-2097 add integration test

This commit is contained in:
Krzysztof Majewski 2018-08-25 14:26:23 +02:00
parent 82ad3bde94
commit cc4b50a0d8
5 changed files with 128 additions and 0 deletions

View File

@ -16,4 +16,19 @@ public class Address {
@Column(name = "ZIP")
private String zipCode;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}

View File

@ -15,8 +15,33 @@ public class Email {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String address;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id")
private Employee employee;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}

View File

@ -18,4 +18,19 @@ public class Employee {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
private List<Email> emails;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Email> getEmails() {
return emails;
}
public void setEmails(List<Email> emails) {
this.emails = emails;
}
}

View File

@ -22,4 +22,20 @@ public class Office {
@JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP")
})
private Address address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.hibernate.joincolumn;
import com.baeldung.hibernate.HibernateUtil;
import java.io.IOException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class JoinColumnIntegrationTest {
private Session session;
private Transaction transaction;
@Before
public void setUp() throws IOException {
session = HibernateUtil.getSessionFactory("hibernate-spatial.properties")
.openSession();
transaction = session.beginTransaction();
}
@After
public void tearDown() {
transaction.rollback();
session.close();
}
@Test
public void givenOfficeEntity_setAddress_shouldPersist() {
Office office = new Office();
Address address = new Address();
address.setZipCode("11-111");
office.setAddress(address);
session.save(office);
session.flush();
session.clear();
}
@Test
public void givenEmployeeEntity_setEmails_shouldPersist() {
Employee employee = new Employee();
Email email = new Email();
email.setAddress("example@email.com");
email.setEmployee(employee);
session.save(employee);
session.flush();
session.clear();
}
}