gpt4 book ai didi

java - hibernate :CasecadeType.ALL vs {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE}

转载 作者:行者123 更新时间:2023-12-01 14:21:33 26 4
gpt4 key购买 nike

我有一个简单的 hibernate OneToOne 程序。它包含一个 导师类(class)和讲师详情 类(class)。
OneToOne 映射定义为从 Instructor 到 InstructorDetail。我只是保存了一个 Instructor 类的对象(由于级联,它也应该保存 InstructorDetail 对象)。场景是:

  • 当我使用 CascadeType.ALL 时,它工作正常。
  • 但是,当我使用具有所有可能的级联类型的数组 ( @OneToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE}) ) 时,它给我以下错误消息:

  • object references an unsaved transient instance - save the transient instance before flushing: com.poc.entity.InstructorDetail



    请在下面找到我的代码:
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToOne;
    import javax.persistence.Table;

    @Entity
    @Table(name="instructor")
    public class Instructor {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name="id")
    private int id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @OneToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE})
    @JoinColumn(name="instructor_detail_id")
    private InstructorDetail instructorDetail;

    public Instructor() {

    }

    public Instructor(String firstName, String lastName) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getFirstName() {
    return firstName;
    }

    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }

    public String getLastName() {
    return lastName;
    }

    public void setLastName(String lastName) {
    this.lastName = lastName;
    }

    public InstructorDetail getInstructorDetail() {
    return instructorDetail;
    }

    public void setInstructorDetail(InstructorDetail instructorDetail) {
    this.instructorDetail = instructorDetail;
    }

    }
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name="instructor_detail")
    public class InstructorDetail {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private int id;

    @Column(name="youtube_channel", length=40)
    private String youtubeChannel;

    @Column(name="hobby", length=20)
    private String hobby;

    public InstructorDetail() {

    }

    public InstructorDetail(String youtubeChannel, String hobby) {
    super();
    this.youtubeChannel = youtubeChannel;
    this.hobby = hobby;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getYoutubeChannel() {
    return youtubeChannel;
    }

    public void setYoutubeChannel(String youtubeChannel) {
    this.youtubeChannel = youtubeChannel;
    }

    public String getHobby() {
    return hobby;
    }

    public void setHobby(String hobby) {
    this.hobby = hobby;
    }




    }

    ..
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;

    import com.poc.entity.Instructor;
    import com.poc.entity.InstructorDetail;

    public class OneToOneDemo {

    private static SessionFactory sessionFactory;

    static {
    sessionFactory = new Configuration().configure("hibernate.cfg.xml")
    .addAnnotatedClass(Instructor.class)
    .addAnnotatedClass(InstructorDetail.class)
    .buildSessionFactory();
    }

    public static void main(String...s) {

    InstructorDetail instructorDetail = new InstructorDetail("xyz/youtube.com","cooking");

    Instructor instructor = new Instructor("Bhupesh","Nimje");

    try {
    instructor.setInstructorDetail(instructorDetail);

    save(instructor);
    }
    finally {
    sessionFactory.close();
    }


    }

    public static void save(Instructor instructor) {
    Session session = null;
    try {
    session = sessionFactory.openSession();

    session.beginTransaction();

    session.save(instructor);

    session.getTransaction().commit();
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    finally {
    session.close();
    }
    }

    }

    最佳答案

    最后我能够破解它。我们使用的 CascadeType 枚举是 javax.persistence 包。 session.save()另一方面,方法是原始的 Hibernate 方法,不符合 JPA 规范,因此存在这种不一致。然而,如果我们使用 session.persist() 方法做同样的事情,它会像魅力一样工作。

    关于java - hibernate :CasecadeType.ALL vs {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47666767/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com