This repository was archived by the owner on Oct 21, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +36
-3
lines changed
main/java/es/msanchez/frameworks/java/spring/boot/entity
test/java/es/msanchez/frameworks/java/spring/boot/hibernate Expand file tree Collapse file tree 3 files changed +36
-3
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ public class Hobby {
17
17
18
18
@ Id
19
19
@ GeneratedValue (strategy = GenerationType .IDENTITY )
20
- private int id ;
20
+ private Long id ;
21
21
22
22
private String name ;
23
23
Original file line number Diff line number Diff line change 3
3
import lombok .Data ;
4
4
import lombok .NoArgsConstructor ;
5
5
6
+ import javax .persistence .CascadeType ;
6
7
import javax .persistence .Entity ;
7
8
import javax .persistence .GeneratedValue ;
8
9
import javax .persistence .GenerationType ;
9
10
import javax .persistence .Id ;
11
+ import javax .persistence .ManyToMany ;
10
12
import javax .persistence .Table ;
13
+ import java .util .List ;
11
14
12
15
@ Table
13
16
@ Entity
@@ -17,10 +20,13 @@ public class Person {
17
20
18
21
@ Id
19
22
@ GeneratedValue (strategy = GenerationType .IDENTITY )
20
- private int id ;
23
+ private Long id ;
21
24
22
25
private String name ;
23
26
24
27
private Integer age ;
25
28
29
+ @ ManyToMany (cascade = CascadeType .PERSIST )
30
+ private List <Hobby > hobbies ;
31
+
26
32
}
Original file line number Diff line number Diff line change 1
1
package es .msanchez .frameworks .java .spring .boot .hibernate ;
2
2
3
3
import es .msanchez .frameworks .java .spring .boot .entity .Hobby ;
4
+ import es .msanchez .frameworks .java .spring .boot .entity .Person ;
4
5
import org .hibernate .Session ;
5
6
import org .testng .annotations .AfterMethod ;
6
7
import org .testng .annotations .BeforeMethod ;
7
8
import org .testng .annotations .Test ;
8
9
10
+ import java .util .ArrayList ;
11
+ import java .util .List ;
12
+
9
13
public class HibernateUtilTest {
10
14
11
15
private Session session ;
@@ -21,7 +25,7 @@ public void tearDown() {
21
25
}
22
26
23
27
@ Test
24
- public void testInsert () {
28
+ public void testInsertHobby () {
25
29
// @GIVEN
26
30
session .beginTransaction ();
27
31
@@ -34,4 +38,27 @@ public void testInsert() {
34
38
35
39
// @THEN
36
40
}
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
+ }
37
64
}
You can’t perform that action at this time.
0 commit comments