`
liutjedu
  • 浏览: 47176 次
  • 性别: Icon_minigender_1
  • 来自: 天津
最近访客 更多访客>>
社区版块
存档分类
最新评论

关于Object references an unsaved transient instance

阅读更多

首先引用以下连接,从中得到答案:

http://www.newbooks.com.cn/info/118573.html

在我自己的实际项目中发生如下问题:

java 代码
  1. try {   
  2.             grpCustomerInfo.setCorporationname(corporationName);   
  3.             grpCustomerInfo.setProvince(province);   
  4.             grpCustomerInfo.setCity(city);   
  5.             grpCustomerInfo.setParentsign(ynoHO);   
  6.             grpCustomerInfo.setOrganizationproperty(organizationProperty);   
  7.             grpCustomerInfo.setParentcustomerid(mainCustomerIDForSub);   
  8.             grpCustomerInfo.setCorporationadds(corporationAdds);   
  9.             grpCustomerInfo.setCorporationproperty(corporationProperty);   
  10.             grpCustomerInfo.setTradetype(tradeType);   
  11.             grpCustomerInfo.setCorporationphone(corporationPhone);   
  12.             grpCustomerInfo.setCorporationfax(corporationFax);   
  13.             grpCustomerInfo.setPostalcode(postalcode);   
  14.             grpCustomerInfo.setFoundationdate(foundationDate);   
  15.             grpCustomerInfo.setSumpersons(Integer.getInteger(sumPersons));             
  16.             grpCustomerInfo.setCustomerid(maincustomerid);   
  17.             //管理机构   
  18.             grpCustomerInfo.setOrganization(organization);   
  19.             //操作者   
  20.             grpCustomerInfo.setOperator(operator);   
  21.             //操作时间   
  22.             grpCustomerInfo.setOperationTime(operationTime);   
  23.             grpCustomerInfo.getNewpolicies().add(this.getNewPolicy(printNO));   
  24.             this.getNewPolicy(printNO).setGrpcustomerinfo(grpCustomerInfo);   
  25.             this.getGrpCustomerInfoDAO().save(grpCustomerInfo);   
  26.             this.getNewPolicyDAO().update(this.getNewPolicy(printNO));   
  27.             //设置表Savenewpolicystate的Savecustomerinofstate字段为1,该字段表示客户信息已经保存   
  28.             //客户信息录入状态   
  29.             this.getNewPolicy(printNO).getSavenewpolicystate().setSavecustomerinofstate("1");              
  30.                
  31.             this.getSaveNewPolicyStateDAO().update(this.getNewPolicy(printNO).getSavenewpolicystate());   
  32.             bl=true;   
  33.         } catch (Exception e) {   
  34.             e.printStackTrace();   
  35.         }   
  36.         return bl;  

 但是如果将24行到29行顺序调整为:

java 代码
  1. this.getNewPolicy(printNO).setGrpcustomerinfo(grpCustomerInfo);   
  2.   
  3. //设置表Savenewpolicystate的Savecustomerinofstate字段为1,该字段表示客户信息已经保存   
  4. //客户信息录入状态   
  5. this.getNewPolicy(printNO).getSavenewpolicystate().setSavecustomerinofstate("1");   
  6.   
  7. this.getGrpCustomerInfoDAO().save(grpCustomerInfo);   
  8. this.getNewPolicyDAO().update(this.getNewPolicy(printNO));   
  9. this.getSaveNewPolicyStateDAO().update(this.getNewPolicy(printNO).getSavenewpolicystate());  

将会发生Object references an unsaved transient instance的问题.

因为"某个对象的某个属性是一个实体,在这个实体没有保存之前就保存这个对象而造成了这个错误。"在以上例子中this.getNewPolicy(printNO)没有保存,就在以下的程序中使用了,this.getNewPolicy(printNO).getSavenewpolicystate().所以出现错误!

分享到:
评论

相关推荐

    毕业设计-物流管理系统的设计与实现(java)

    3.Caused by:org.hibernate.TransientObjectException:object references an unsaved transient instance 原因:代码中关联的对象没有set值进去。查看代码,发现是enterPriseStrength和enterprise为多对一

    制作调试过程及数据记录1

    在调试过程中,可能会遇到`Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance`这样的异常。这个异常通常是因为在持久化操作时,关联的对象没有被正确设置。例如...

    JPA 开发中遇到的错误

    org.hibernate.TransientObjectException: object references an unsaved transient instance 当一个实体引用了另一个尚未保存的实体时,会抛出此类异常。这通常发生在级联保存的场景中,如果父实体尝试保存时,子...

    Hibernate映射导致的几个异常

    TransientObjectException: object references an unsaved transient instance 这个异常通常发生在级联操作中,例如当配置了`cascade="save-update,persist"`。如果在保存一个对象时,该对象引用了一个未保存的...

    hibernate常见错误解决方案

    **TransientObjectException: object references an unsaved transient instance** **异常描述:** 此异常表示一个实体引用了另一个尚未保存的瞬态实体。 **解决方法:** - 在保存实体之前先保存所有关联的瞬态...

    java 遇到的各种异常

    `org.hibernate.TransientObjectException`: object references an unsaved transient instance** - **异常描述**:当尝试保存一个对象时,该对象引用了一个尚未被持久化的对象。 - **原因分析**:通常发生在尝试...

Global site tag (gtag.js) - Google Analytics