org.hibernate.TransientObjectException:object引用未保存的瞬态实例

时间:2014-05-12 08:53:26

标签: java hibernate orm struts2

此处我尝试更新SupplierPaidDetail包括(supplierPaidOnlinessupplierPaidCashessupplierPaidChecks)中的数据,以下是我的代码,但这显示异常。

Jsp Page

<s:form action="UpdatedPaid" method="post">  
    <s:if test="paidList.size>0"> 
        <s:iterator value="paidList" var="pdlst">
            <div class="box">
                <span class="label">Payment Date</span>
                <span class="ib">
                    <s:hidden name="id" id="paidId" value="%{paidId}"/>
                    <s:hidden name="supplierPaymentDetails.id" value="%{supplierPaymentId}"/>
                    <s:textfield name="paymentDate" id="paymentDate" value="%{paymentDate}"/>
                </span>
            </div>
            <div class="box">
                <span class="label">Payment Mode</span>
                <span class="ib">
                    <s:textfield name="paymentMode" id="payment_mode" readonly="true" value="%{payment_mode}"/>
                </span>
            </div>
            <s:if test="cashActionList.size>0"> 
                <s:iterator value="cashActionList" status="rowstatus">
                    <div class="box">
                        <span class="label">Amount</span>
                        <span class="ib"> 
                            <s:textfield name="supplierPaidCashes[%{#rowstatus.index}].amount" id="amount" value="%{amount}"/>
                        </span>
                    </div>
                    <div class="box">
                        <span class="label">TO Person</span>
                        <span class="ib">
                            <s:hidden name="supplierPaidCashes[%{#rowstatus.index}].id" id="id" value="%{id}"/>
                            <s:textfield name="supplierPaidCashes[%{#rowstatus.index}].toPerson" id="toPerson" value="%{toPerson}"/>
                        </span>
                    </div>
                    <div class="box">
                        <span class="label">Designation</span>
                        <span class="ib">
                            <s:textfield name="supplierPaidCashes[%{#rowstatus.index}].Designation" id="Designation" value="%{Designation}"/>
                        </span>
                    </div>
                    <div class="box">
                        <span class="label">Receipt No</span>
                        <span class="ib">
                            <s:textfield name="supplierPaidCashes[%{#rowstatus.index}].receiptNo" id="receiptNo" value="%{reciptNo}"/>
                        </span>
                    </div>
                </s:iterator>
            </s:if>
        </s:iterator>
    </s:if>
</s:form>

在行动中

public class hbmCashAction extends ActionSupport implements ModelDriven<SupplierPaidDetails> {

    SupplierPaidDetails spa = new SupplierPaidDetails();

    public String update() {
        System.out.println("--------");

        System.out.println("id" + spa.getId() + " " + spa.getPaymentDate());
        SupplierPaymentDetails spdd = new SupplierPaymentDetails();
        spa.setSupplierPaymentDetails(spdd);
        List<SupplierPaidCash> aca = spa.getSupplierPaidCashes();
        for (SupplierPaidCash ss : aca) {
            System.out.println("Cash Details");
            ss.setSupplierPaidDetails(spa);
            System.out.println(ss.getId() + " " + ss.getAmount() + " " + ss.getToPerson());
        }
        List<SupplierPaidCheck> ax = spa.getSupplierPaidChecks();
        for (SupplierPaidCheck ss : ax) {
            System.out.println("Check Details");

            ss.setSupplierPaidDetails(spa);
            System.out.println(ss.getId() + " " + ss.getPaidToAccNo() + " " + ss.getPayableAt());
        }
        List<SupplierPaidOnline> onl = spa.getSupplierPaidOnlines();
        for (SupplierPaidOnline ss : onl) {
            System.out.println("Online Details");
            ss.setSupplierPaidDetails(spa);
            System.out.println(ss.getId() + " " + ss.getPaidToAccNo() + " " + ss.getPayRefNo());
        }
        System.out.println("--------");
        CashDao co = new CashDao();
        co.update(spa);
        return SUCCESS;
    }
  }

DAO

public class CashDao {

    public void update(SupplierPaidDetails spd) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        try {
            session.update(spd);
        } catch (HibernateException e) {
            System.out.println("Exception  " + e);
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();

    }
  }

SupplierPaidDetalis

public class SupplierPaidDetails implements java.io.Serializable {

    private Long id;
    private SupplierPaymentDetails supplierPaymentDetails;
    private String paymentMode;
    private Date paymentDate;
    private Date addDate;
    private String status;
    private List<SupplierPaidOnline> supplierPaidOnlines = new ArrayList();
    private List<SupplierPaidCash> supplierPaidCashes = new ArrayList();
    private List<SupplierPaidCheck> supplierPaidChecks = new ArrayList();
 /getter and setter
 }

supplierPaidDetails.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated May 10, 2014 11:18:04 PM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
    <class name="iland.hbm.SupplierPaidDetails" table="supplier_paid_details" catalog="retail_shop" dynamic-update="true">
        <id name="id" type="java.lang.Long">
            <column name="id" />
            <generator class="identity" />
        </id>
        <many-to-one name="supplierPaymentDetails" class="iland.hbm.SupplierPaymentDetails" fetch="select">
            <column name="supp_payment_id" not-null="true" />
        </many-to-one>
        <property name="paymentMode" type="string">
            <column name="payment_mode" length="20" />
        </property>
        <property name="paymentDate" type="date">
            <column name="payment_date" length="10" />
        </property>
        <property name="addDate" type="timestamp">
            <column name="add_date" length="19" default="0" />
        </property>
        <property name="status" type="string">
            <column name="status" length="20" />
        </property>
        <list name="supplierPaidOnlines" table="supplier_paid_online" inverse="true" lazy="true" fetch="select" cascade="all" >
            <key column="spd_id"  />
            <list-index column="id" />
            <one-to-many class="iland.hbm.SupplierPaidOnline" />
        </list>
        <list name="supplierPaidCashes" table="supplier_paid_cash" inverse="true" lazy="true" fetch="select" cascade="all">
            <key column="spd_id"  />
            <list-index column="id" />
            <one-to-many class="iland.hbm.SupplierPaidCash" />
        </list>
        <list name="supplierPaidChecks" table="supplier_paid_check" inverse="true" lazy="true" fetch="select" cascade="all">
            <key column="spd_id"  />
            <list-index column="id" />
            <one-to-many class="iland.hbm.SupplierPaidCheck" />
        </list>
    </class>
</hibernate-mapping>

例外

SEVERE: Exception occurred during processing request: object references an unsaved transient instance - save the transient instance before flushing: iland.hbm.SupplierPaymentDetails
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: iland.hbm.SupplierPaymentDetails
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:121)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2184)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2559)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2495)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2822)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at iland.payment.CashDao.update(CashDao.java:57)
    at iland.payment.hbmCashAction.update(hbmCashAction.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)

如何解决上述异常

1 个答案:

答案 0 :(得分:0)

update hibernate要求所有对象都是持久的,因此您必须从会话中get / load SupplierPaymentDetails并设置为SupplierPaidDetails。如果通过id获取第一个实体可用的对象,则可以执行此操作。要获取第二个实体,您需要在DAO中编写一个具有id参数的方法并返回实体对象。如果您手动管理会话和事务,则必须在那里打开新会话和事务。