File tree Expand file tree Collapse file tree
main/java/com/baeldung/hibernate/joincolumn
test/java/com/baeldung/hibernate/joincolumn Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,4 +16,19 @@ public class Address {
1616 @ Column (name = "ZIP" )
1717 private String zipCode ;
1818
19+ public Long getId () {
20+ return id ;
21+ }
22+
23+ public void setId (Long id ) {
24+ this .id = id ;
25+ }
26+
27+ public String getZipCode () {
28+ return zipCode ;
29+ }
30+
31+ public void setZipCode (String zipCode ) {
32+ this .zipCode = zipCode ;
33+ }
1934}
Original file line number Diff line number Diff line change @@ -15,8 +15,33 @@ public class Email {
1515 @ GeneratedValue (strategy = GenerationType .AUTO )
1616 private Long id ;
1717
18+ private String address ;
19+
1820 @ ManyToOne (fetch = FetchType .LAZY )
1921 @ JoinColumn (name = "employee_id" )
2022 private Employee employee ;
2123
24+ public Long getId () {
25+ return id ;
26+ }
27+
28+ public void setId (Long id ) {
29+ this .id = id ;
30+ }
31+
32+ public String getAddress () {
33+ return address ;
34+ }
35+
36+ public void setAddress (String address ) {
37+ this .address = address ;
38+ }
39+
40+ public Employee getEmployee () {
41+ return employee ;
42+ }
43+
44+ public void setEmployee (Employee employee ) {
45+ this .employee = employee ;
46+ }
2247}
Original file line number Diff line number Diff line change @@ -18,4 +18,19 @@ public class Employee {
1818 @ OneToMany (fetch = FetchType .LAZY , mappedBy = "employee" )
1919 private List <Email > emails ;
2020
21+ public Long getId () {
22+ return id ;
23+ }
24+
25+ public void setId (Long id ) {
26+ this .id = id ;
27+ }
28+
29+ public List <Email > getEmails () {
30+ return emails ;
31+ }
32+
33+ public void setEmails (List <Email > emails ) {
34+ this .emails = emails ;
35+ }
2136}
Original file line number Diff line number Diff line change @@ -22,4 +22,20 @@ public class Office {
2222 @ JoinColumn (name ="ADDR_ZIP" , referencedColumnName ="ZIP" )
2323 })
2424 private Address address ;
25+
26+ public Long getId () {
27+ return id ;
28+ }
29+
30+ public void setId (Long id ) {
31+ this .id = id ;
32+ }
33+
34+ public Address getAddress () {
35+ return address ;
36+ }
37+
38+ public void setAddress (Address address ) {
39+ this .address = address ;
40+ }
2541}
Original file line number Diff line number Diff line change 1+ package com.baeldung.hibernate.joincolumn;
2+
3+ import com.baeldung.hibernate.HibernateUtil;
4+ import java.io.IOException;
5+ import org.hibernate.Session;
6+ import org.hibernate.Transaction;
7+
8+ import org.junit.After;
9+ import org.junit.Before;
10+ import org.junit.Test;
11+
12+
13+ public class JoinColumnIntegrationTest {
14+
15+ private Session session;
16+ private Transaction transaction;
17+
18+ @Before
19+ public void setUp() throws IOException {
20+ session = HibernateUtil.getSessionFactory("hibernate-spatial.properties")
21+ .openSession();
22+ transaction = session.beginTransaction();
23+ }
24+
25+ @After
26+ public void tearDown() {
27+ transaction.rollback();
28+ session.close();
29+ }
30+
31+ @Test
32+ public void givenOfficeEntity_setAddress_shouldPersist() {
33+ Office office = new Office();
34+
35+ Address address = new Address();
36+ address.setZipCode("11-111");
37+ office.setAddress(address);
38+
39+ session.save(office);
40+ session.flush();
41+ session.clear();
42+ }
43+
44+ @Test
45+ public void givenEmployeeEntity_setEmails_shouldPersist() {
46+ Employee employee = new Employee();
47+
48+ Email email = new Email();
49+ email.setAddress("example@email.com");
50+ email.setEmployee(employee);
51+
52+ session.save(employee);
53+ session.flush();
54+ session.clear();
55+ }
56+
57+ }
You can’t perform that action at this time.
0 commit comments