Home > AI > Backend > SpringBoot > mysql-connector-java >

OneToMany save

Child1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Data
@Entity
public class Child1 {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Parent1 parent;
 
    @Column(name="name")
    private String name;
 
    public Child1(String name){
        this.name = name;
    }
}

Parent1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Data
@Entity
public class Parent1 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    private List<Child1> children;
 
    @Column(name="name")
    private String name;
 
    public Parent1(String name){
        this.name = name;
    }
}

Child2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Data
@Entity
public class Child2 {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Parent2 parent;
 
    @Column(name="name")
    private String name;
 
    public Child2(String name){
        this.name = name;
    }
}

Parent2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Data
@Entity
public class Parent2 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    @OneToMany(mappedBy = "parent", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE,
            CascadeType.REFRESH, CascadeType.DETACH })
    private List<Child2> children;
 
    @Column(name="name")
    private String name;
 
 
    public Parent2(String name){
        this.name = name;
    }
}

Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class Test1 {
    private SessionFactory sessionFactory;
 
    @Before
    public void setUp() throws Exception {
        // A SessionFactory is set up once for an application
        sessionFactory = new Configuration()
                .configure() // configures settings from hibernate.cfg.xml
                .buildSessionFactory();
    }
 
    @After
    public void tearDown() throws Exception {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
    }
 
    @Test
    public void testCascadeTypeAll() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
 
        Parent1 parent = new Parent1("Senior Mary");
        Child1 child = new Child1("Junior Mary");
 
        child.setParent(parent);
        parent.setChildren(Arrays.asList(child));
 
 
        session.save(parent);
 
        Assert.assertNotNull(parent.getId());
        Assert.assertNotNull(child.getId());
 
        session.getTransaction().commit();
        session.close();
    }
 
    @Test
    public void testCascadeTypeOthers() {
        // create a couple of events...
        Session session = sessionFactory.openSession();
        session.beginTransaction();
 
        Parent2 parent = new Parent2("SeniorPeter");
        Child2 child = new Child2("JuniorPeter");
        child.setParent(parent);
        parent.setChildren(Arrays.asList(child));
 
        session.save(parent);
 
        Assert.assertNotNull(parent.getId());
        //here we fail
        Assert.assertNotNull(child.getId());
 
        session.getTransaction().commit();
        session.close();
    }
 
}

resources/hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 
<hibernate-configuration>
 
    <session-factory>
 
        <!-- Database connection settings -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
 
 
        <!-- JDBC connection pool (use the built-in) -->
        <property name="hibernate.connection.pool_size">1</property>
 
        <!-- Disable the second-level cache -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 
 
 
 
        <mapping class="com.example.demo11.Parent1" />
        <mapping class="com.example.demo11.Parent2" />
        <mapping class="com.example.demo11.Child1" />
        <mapping class="com.example.demo11.Child2" />
 
 
 
 
    </session-factory>
 
</hibernate-configuration>

Leave a Reply