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

OneToMany save

Child1.java

@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

@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

@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

@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

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

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<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