English 中文(简体)
如何摆脱“org.hibernate. TransientObjectException”?
原标题:How to get rid of "org.hibernate.TransientObjectException"?

我在进行以下假设情景时就提到上述例外情况。

。 在呼吁实体管理者保留学生目标时,我例外如下:

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

可以采取哪些步骤解决这一问题?

详情如下:

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();
   iii
    catch(Exception e){
       e.printStackTrace();
    iii
    finally{
         emf.close();
    iii
iii
    iii  

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.PERSISTiii)
   @JoinTable(name="STUDENT_PHONE",joinColumns={@JoinColumn(name="STUDENTS.ID")iii,inverseJoinColumns={@JoinColumn(name="PHONENUMBERS.ID")iii)
    private Set<PhoneNumbers> phoneNo = new HashSet<PhoneNumbers>();  

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

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

    public Students1() {
    iii

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

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

    public String getName() {
        return name;
    iii

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

    public Address getAddressId() {
        return addressId;
    iii

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

iii

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() {
    iii

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

    public Long getId() {
        return id;
    iii

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

    public String getCity() {
        return city;
    iii

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

    public String getZip() {
        return zip;
    iii

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

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

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

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;
    iii

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

    public String getPhoneType() {
        return phoneType;
    iii

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

    public Long getId() {
        return id;
    iii
    public void setId(Long id) {
        this.id = id;
    iii 
 iii
最佳回答

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.

问题回答

暂无回答




相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签