Skip to content

Commit 0d56c80

Browse files
authored
Merge pull request eugenp#5016 from MajewskiKrzysztof/BAEL-2097
BAEL-2056
2 parents 8a65afe + 86b279e commit 0d56c80

5 files changed

Lines changed: 215 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.hibernate.joincolumn;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Address {
11+
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.AUTO)
14+
private Long id;
15+
16+
@Column(name = "ZIP")
17+
private String zipCode;
18+
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+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.hibernate.joincolumn;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.FetchType;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.ManyToOne;
10+
11+
@Entity
12+
public class Email {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
private Long id;
17+
18+
private String address;
19+
20+
@ManyToOne(fetch = FetchType.LAZY)
21+
@JoinColumn(name = "employee_id")
22+
private Employee employee;
23+
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+
}
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.hibernate.joincolumn;
2+
3+
import java.util.List;
4+
import javax.persistence.Entity;
5+
import javax.persistence.FetchType;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.OneToMany;
10+
11+
@Entity
12+
public class Employee {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
private Long id;
17+
18+
@OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
19+
private List<Email> emails;
20+
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+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.hibernate.joincolumn;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.FetchType;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.JoinColumns;
10+
import javax.persistence.ManyToOne;
11+
12+
@Entity
13+
public class Office {
14+
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Long id;
18+
19+
@ManyToOne(fetch = FetchType.LAZY)
20+
@JoinColumns({
21+
@JoinColumn(name="ADDR_ID", referencedColumnName="ID"),
22+
@JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP")
23+
})
24+
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+
}
41+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

0 commit comments

Comments
 (0)