@Embeddable与@ManyToOne休眠和CascadeType.ALL

时间:2018-06-27 18:22:46

标签: hibernate

我正在尝试使用@EmbeddedId实现Composite标识符。在运行以下主要方法dummy1表时,已插入数据。在dummy3表中没有插入任何记录。将数据插入dummy3表需要哪些更改?请找到以下类别供您参考。

Dummy1.java

@Entity(name="Dummy1")
public class Dummy1 {

    private String name;

    @EmbeddedId
    private Dummy2 dummy2;

     //Getters and setters are omitted for brevity
}

Dummy2.java

@Embeddable
public class Dummy2 implements Serializable {

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="id")
    private Dummy3 d3;

    @Column(name="State")
    private String state;

    @Column(name="city")
    private String city;
//Getters and setters are omitted for brevity

}

Dummy3.java

@Entity(name="Dummy3")
public class Dummy3 {

    @Id
    private int id;

    private String name;

    private String lastName;

//Getters and setters are omitted for brevity
}

Main.java

public static void main(String[] args) {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure("/resources/hibernate.cfg.xml")
                .build();

        factory = new MetadataSources(registry)
                .buildMetadata()
                .buildSessionFactory();

        session=factory.openSession();


        session.getTransaction().begin();

        Dummy1 a=new Dummy1();
        a.setName("aaaa");

        Dummy2 b=new Dummy2();
        b.setCity("aaa");
        b.setState("bbb");

        Dummy3 c=new Dummy3();
        c.setId(1);
        c.setName("ddd");
        c.setLastName("dddd");

        b.setD3(c);
        a.setDummy2(b);

        session.persist(a);
        session.getTransaction().commit();
        session.close();

    }

0 个答案:

没有答案