如何摆脱“org.hibernate.TransientObjectException”?

发布于 2024-12-12 11:45:16 字数 5066 浏览 0 评论 0原文

在执行以下场景时,我遇到了上述异常。

学生地址具有多对一关系,其中学生电话号码具有一对多关系。在调用 EntityManager 对象上的 persist 方法来保存“学生”对象时,出现如下异常:

org.hibernate.TransientObjectException:对象引用未保存的对象 瞬态实例 - 在刷新之前保存瞬态实例: model.Students1.addressId ->型号.地址

可以采取什么步骤来解决?

详细信息如下:

DAO 类 :

    public class DAO {

    public static void main(String[] arr){

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneToManyPU");

    EntityManager em = emf.createEntityManager();

    EntityTransaction tr= em.getTransaction();

    try{
        tr.begin();

        PhoneNumbers p1 = new PhoneNumbers();
        PhoneNumbers p2 = new PhoneNumbers();

        p1.setPhoneType("mobile");
        p1.setPhoneNo("9881592106");

        p2.setPhoneType("landline");
        p2.setPhoneNo("24214988");

        Set<PhoneNumbers> phones = new HashSet<PhoneNumbers>();
        phones.add(p1);
        phones.add(p2);

        em.persist(p1);
        em.persist(p2);

        Address a1 = new Address();
        a1.setCity("Pune");
        a1.setZip("400987");
        Students1 s1 = new Students1();
        s1.setName("Alka");
        s1.setAddressId(a1);
        s1.setPhoneNo(phones);

        em.persist(s1);
         tr.commit();
   }
    catch(Exception e){
       e.printStackTrace();
    }
    finally{
         emf.close();
    }
}
    }  

Students1 类:

    @Entity    
@Table(name = "STUDENTS")    
public class Students1 implements Serializable {    
    private static final long serialVersionUID = 1L;     
    @Id    
    @GeneratedValue(strategy = GenerationType.AUTO)     
    @Column(name = "ID")    
    private Long id;    

    @Column(name = "NAME")    
    private String name;    

    @JoinColumn(name = "ADDRESS_ID", referencedColumnName = "ID")    
    @ManyToOne    
    private Address addressId;  

    @OneToMany(cascade ={CascadeType.MERGE,CascadeType.PERSIST})
   @JoinTable(name="STUDENT_PHONE",joinColumns={@JoinColumn(name="STUDENTS.ID")},inverseJoinColumns={@JoinColumn(name="PHONENUMBERS.ID")})
    private Set<PhoneNumbers> phoneNo = new HashSet<PhoneNumbers>();  

       public void setPhoneNo(Set<PhoneNumbers> phoneNo) {  
        this.phoneNo = phoneNo;
    }

    public Set<PhoneNumbers> getPhoneNo() {
        return phoneNo;
    }

    public Students1() {
    }

    public Students1(Long id) {
        this.id = id;
    }
    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddressId() {
        return addressId;
    }

    public void setAddressId(Address addressId) {
        this.addressId = addressId;
    }

}

Address 类

@Entity  
@Table(name = "ADDRESS")  
public class Address implements Serializable {  
    private static final long serialVersionUID = 1L;  
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO)  
    @Column(name = "ID")  
    private Long id;  
    @Column(name = "CITY")  
    private String city;  
    @Column(name = "ZIP")  
    private String zip;  
    @OneToMany(mappedBy = "addressId")  
    private Collection<Students1> students1Collection;  

    public Address() {
    }

    public Address(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public Collection<Students1> getStudents1Collection() {
        return students1Collection;
    }

    public void setStudents1Collection(Collection<Students1> students1Collection) {
        this.students1Collection = students1Collection;
    }  
}

PhoneNumbers 类

    @Entity 
    public class PhoneNumbers implements Serializable {    
    private static final long serialVersionUID = 1L;    
    @Id     
    @GeneratedValue(strategy = GenerationType.AUTO)    
    private Long id;    

    @Column(name="PhoneNo")  
    private String phoneNo;

    @Column(name="PhoneType")  
    private String phoneType;

    public String getPhoneNo() {  
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {  
        this.phoneNo = phoneNo;
    }

    public String getPhoneType() {
        return phoneType;
    }

    public void setPhoneType(String phoneType) {
        this.phoneType = phoneType;
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    } 
 }

I got the above mentioned exception when performing following scenario.

Students and Address have Many-To-One relationship where as Student and PhoneNumbers have One-To-Many relationship. On calling persist method on EntityManager object for saving 'Students' object, I got exception as follows:

org.hibernate.TransientObjectException: object references an unsaved
transient instance - save the transient instance before flushing:
model.Students1.addressId -> model.Address

What steps can be taken to resolve it?

Details are as follows:

DAO class :

    public class DAO {

    public static void main(String[] arr){

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneToManyPU");

    EntityManager em = emf.createEntityManager();

    EntityTransaction tr= em.getTransaction();

    try{
        tr.begin();

        PhoneNumbers p1 = new PhoneNumbers();
        PhoneNumbers p2 = new PhoneNumbers();

        p1.setPhoneType("mobile");
        p1.setPhoneNo("9881592106");

        p2.setPhoneType("landline");
        p2.setPhoneNo("24214988");

        Set<PhoneNumbers> phones = new HashSet<PhoneNumbers>();
        phones.add(p1);
        phones.add(p2);

        em.persist(p1);
        em.persist(p2);

        Address a1 = new Address();
        a1.setCity("Pune");
        a1.setZip("400987");
        Students1 s1 = new Students1();
        s1.setName("Alka");
        s1.setAddressId(a1);
        s1.setPhoneNo(phones);

        em.persist(s1);
         tr.commit();
   }
    catch(Exception e){
       e.printStackTrace();
    }
    finally{
         emf.close();
    }
}
    }  

Students1 class:

    @Entity    
@Table(name = "STUDENTS")    
public class Students1 implements Serializable {    
    private static final long serialVersionUID = 1L;     
    @Id    
    @GeneratedValue(strategy = GenerationType.AUTO)     
    @Column(name = "ID")    
    private Long id;    

    @Column(name = "NAME")    
    private String name;    

    @JoinColumn(name = "ADDRESS_ID", referencedColumnName = "ID")    
    @ManyToOne    
    private Address addressId;  

    @OneToMany(cascade ={CascadeType.MERGE,CascadeType.PERSIST})
   @JoinTable(name="STUDENT_PHONE",joinColumns={@JoinColumn(name="STUDENTS.ID")},inverseJoinColumns={@JoinColumn(name="PHONENUMBERS.ID")})
    private Set<PhoneNumbers> phoneNo = new HashSet<PhoneNumbers>();  

       public void setPhoneNo(Set<PhoneNumbers> phoneNo) {  
        this.phoneNo = phoneNo;
    }

    public Set<PhoneNumbers> getPhoneNo() {
        return phoneNo;
    }

    public Students1() {
    }

    public Students1(Long id) {
        this.id = id;
    }
    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddressId() {
        return addressId;
    }

    public void setAddressId(Address addressId) {
        this.addressId = addressId;
    }

}

Address Class

@Entity  
@Table(name = "ADDRESS")  
public class Address implements Serializable {  
    private static final long serialVersionUID = 1L;  
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO)  
    @Column(name = "ID")  
    private Long id;  
    @Column(name = "CITY")  
    private String city;  
    @Column(name = "ZIP")  
    private String zip;  
    @OneToMany(mappedBy = "addressId")  
    private Collection<Students1> students1Collection;  

    public Address() {
    }

    public Address(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public Collection<Students1> getStudents1Collection() {
        return students1Collection;
    }

    public void setStudents1Collection(Collection<Students1> students1Collection) {
        this.students1Collection = students1Collection;
    }  
}

PhoneNumbers class

    @Entity 
    public class PhoneNumbers implements Serializable {    
    private static final long serialVersionUID = 1L;    
    @Id     
    @GeneratedValue(strategy = GenerationType.AUTO)    
    private Long id;    

    @Column(name="PhoneNo")  
    private String phoneNo;

    @Column(name="PhoneType")  
    private String phoneType;

    public String getPhoneNo() {  
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {  
        this.phoneNo = phoneNo;
    }

    public String getPhoneType() {
        return phoneType;
    }

    public void setPhoneType(String phoneType) {
        this.phoneType = phoneType;
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    } 
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

究竟谁懂我的在乎 2024-12-19 11:45:17

您应该定义级联。
级联意味着如果对象“A”具有对象“B”(或这些对象的集合),则引用的“B”对象将按照级联定义的指示进行保存/更新。

You should define cascades.
A cascade means that if object 'A' has an Object 'B' (or a collection of those) then the referenced 'B' objects are saved/updated as your cascade definition indicates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文