Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 3bbb4df

Browse files
committed
hibernate: added intermediary table
1 parent 069c83c commit 3bbb4df

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

hibernate/app/src/main/java/es/msanchez/frameworks/java/spring/boot/entity/Hobby.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Hobby {
1717

1818
@Id
1919
@GeneratedValue(strategy = GenerationType.IDENTITY)
20-
private int id;
20+
private Long id;
2121

2222
private String name;
2323

hibernate/app/src/main/java/es/msanchez/frameworks/java/spring/boot/entity/Person.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import lombok.Data;
44
import lombok.NoArgsConstructor;
55

6+
import javax.persistence.CascadeType;
67
import javax.persistence.Entity;
78
import javax.persistence.GeneratedValue;
89
import javax.persistence.GenerationType;
910
import javax.persistence.Id;
11+
import javax.persistence.ManyToMany;
1012
import javax.persistence.Table;
13+
import java.util.List;
1114

1215
@Table
1316
@Entity
@@ -17,10 +20,13 @@ public class Person {
1720

1821
@Id
1922
@GeneratedValue(strategy = GenerationType.IDENTITY)
20-
private int id;
23+
private Long id;
2124

2225
private String name;
2326

2427
private Integer age;
2528

29+
@ManyToMany(cascade = CascadeType.PERSIST)
30+
private List<Hobby> hobbies;
31+
2632
}

hibernate/app/src/test/java/es/msanchez/frameworks/java/spring/boot/hibernate/HibernateUtilTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package es.msanchez.frameworks.java.spring.boot.hibernate;
22

33
import es.msanchez.frameworks.java.spring.boot.entity.Hobby;
4+
import es.msanchez.frameworks.java.spring.boot.entity.Person;
45
import org.hibernate.Session;
56
import org.testng.annotations.AfterMethod;
67
import org.testng.annotations.BeforeMethod;
78
import org.testng.annotations.Test;
89

10+
import java.util.ArrayList;
11+
import java.util.List;
12+
913
public class HibernateUtilTest {
1014

1115
private Session session;
@@ -21,7 +25,7 @@ public void tearDown() {
2125
}
2226

2327
@Test
24-
public void testInsert() {
28+
public void testInsertHobby() {
2529
// @GIVEN
2630
session.beginTransaction();
2731

@@ -34,4 +38,27 @@ public void testInsert() {
3438

3539
// @THEN
3640
}
41+
42+
@Test
43+
public void testInsertPerson() {
44+
// @GIVEN
45+
session.beginTransaction();
46+
47+
final List<Hobby> hobbies = new ArrayList<>();
48+
final Hobby hobby = new Hobby();
49+
hobby.setName("Climbing");
50+
hobbies.add(hobby);
51+
52+
final Person person = new Person();
53+
person.setName("Mario");
54+
person.setAge(1);
55+
person.setHobbies(hobbies);
56+
57+
// @WHEN
58+
session.save(hobby);
59+
session.save(person);
60+
session.getTransaction().commit();
61+
62+
// @THEN
63+
}
3764
}

0 commit comments

Comments
 (0)