hibernate中的TransientObjectException

时间:2017-08-28 21:20:08

标签: java spring hibernate spring-mvc jpa

发送POST后

  

curl localhost:8888 / bills / addbill -H" Content-Type:application / json" -X POST -d' {" number":" 111A111"," customer":" Customer Cuustomer Rrrr",&# 34;电话":" 1 800 5551212","经理":"经理经理","日期":&#34 ; 2012-09-17"," curId":{" id":" 1"},"付款":[{ " id":" 1"," name":" pomyit stekla"," price":" 1000.0"}]}'

我明白了:

  

{" timestamp":1503954247880," status":500," error":" Internal Server Error"," exception":" org.springframework.dao.InvalidDataAccessApiUsageException"," message":" org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:ru.test.practice.model.Payment;嵌套异常是java.lang.IllegalStateException:org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:ru.test.practice.model.Payment"," path" :" /票据/ addbill"}

我的实体中有多对多的关系"比尔",这里是引发错误的字段。

比尔实体:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.docusign.net/API/3.0">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:CreateAndSendEnvelope>
         <ns:Envelope>
           <ns:AccountId>?</ns:AccountId>
            <ns:Documents>
               <ns:Document>
                <ns:ID>32093411</ns:ID>
                <ns:Name>Challenge3.docx</ns:Name>
                <ns:PDFBytes>?</ns:PDFBytes>
                <ns:FileExtension>pdf</ns:FileExtension>
            </ns:Document>
            </ns:Documents>
            <ns:Recipients>
               <ns:Recipient>
                  <ns:ID>45399085</ns:ID>
                  <ns:UserName>Test Radio Name</ns:UserName>
                  <ns:Email>abc@gmail.com</ns:Email>
                  <ns:Type>Signer</ns:Type>
                  <ns:RoutingOrder>1</ns:RoutingOrder>
               </ns:Recipient>
            </ns:Recipients>
            <ns:Tabs>
              <ns:Tab>
                <ns:DocumentID>32093411</ns:DocumentID>
                <ns:RecipientID>45399085</ns:RecipientID>
                <ns:PageNumber>1</ns:PageNumber>
                <ns:XPosition>285</ns:XPosition>
                <ns:YPosition>304</ns:YPosition>
                <ns:Type>Custom</ns:Type>
                <ns:Name>Yes</ns:Name>
                <ns:TabLabel>Paymentmethod</ns:TabLabel>
                <ns:CustomTabType>Radio</ns:CustomTabType>
                <ns:CustomTabRequired>true</ns:CustomTabRequired>
                <ns:CustomTabLocked>false</ns:CustomTabLocked>
                <ns:SharedTab>false</ns:SharedTab>
             </ns:Tab>
             <ns:Tab>
                <ns:DocumentID>32093411</ns:DocumentID>
                <ns:RecipientID>45399085</ns:RecipientID>
                <ns:PageNumber>1</ns:PageNumber>
                <ns:XPosition>285</ns:XPosition>
                <ns:YPosition>324</ns:YPosition>
                <ns:Type>Custom</ns:Type>
                <ns:Name>No</ns:Name>
                <ns:TabLabel>Paymentmethod</ns:TabLabel>
                <ns:CustomTabType>Radio</ns:CustomTabType>
                <ns:CustomTabRequired>true</ns:CustomTabRequired>
                <ns:CustomTabLocked>false</ns:CustomTabLocked>
            </ns:Tab>
            <ns:Tab>
                <ns:DocumentID>32093411</ns:DocumentID>
                <ns:RecipientID>45399085</ns:RecipientID>
                <ns:PageNumber>1</ns:PageNumber>
                <ns:XPosition>374</ns:XPosition>
                <ns:YPosition>302</ns:YPosition>
                <ns:ScaleValue>10.0</ns:ScaleValue>
                <ns:Type>Custom</ns:Type>
                <ns:TabLabel>Text 85a340b8-8159-420c-810a-76926f8c6dad</ns:TabLabel>
                <ns:CustomTabType>Text</ns:CustomTabType>
                <ns:CustomTabWidth>78</ns:CustomTabWidth>
                <ns:CustomTabHeight>11</ns:CustomTabHeight>
                <ns:CustomTabRequired>true</ns:CustomTabRequired>
                <ns:CustomTabLocked>false</ns:CustomTabLocked>
            <ns:CustomTabDisableAutoSize>false</ns:CustomTabDisableAutoSize>

        <ns:ConditionalParentLabel>Paymentmethod</ns:ConditionalParentLabel>
                <ns:ConditionalParentValue>Yes</ns:ConditionalParentValue>
                <ns:Font>LucidaConsole</ns:Font>
                <ns:Bold>false</ns:Bold>
                <ns:Italic>false</ns:Italic>
                <ns:Underline>false</ns:Underline>
                <ns:FontColor>Black</ns:FontColor>
                <ns:FontSize>Size9</ns:FontSize>
                <ns:IncludeNoteInEmail>false</ns:IncludeNoteInEmail>
                <ns:IsPaymentAmount>false</ns:IsPaymentAmount>
                <ns:RoundDecimalPlaces>0</ns:RoundDecimalPlaces>
                <ns:MaxLength>4000</ns:MaxLength>
                <ns:RequireAll>false</ns:RequireAll>
            </ns:Tab>
            <ns:Tab>
                <ns:DocumentID>32093411</ns:DocumentID>
                <ns:RecipientID>45399085</ns:RecipientID>
                <ns:PageNumber>1</ns:PageNumber>
                <ns:XPosition>376</ns:XPosition>
                <ns:YPosition>366</ns:YPosition>
                <ns:ScaleValue>1.0</ns:ScaleValue>
                <ns:Type>Custom</ns:Type>
                <ns:TabLabel>Text 5928b009-b2af-4bdb-beb5-62c803fdae0b</ns:TabLabel>
                <ns:CustomTabType>Text</ns:CustomTabType>
                <ns:CustomTabWidth>78</ns:CustomTabWidth>
                <ns:CustomTabHeight>11</ns:CustomTabHeight>
                <ns:CustomTabRequired>true</ns:CustomTabRequired>
                <ns:CustomTabLocked>false</ns:CustomTabLocked>
            <ns:CustomTabDisableAutoSize>false</ns:CustomTabDisableAutoSize>
                <ns:TemplateLocked>false</ns:TemplateLocked>
                <ns:TemplateRequired>false</ns:TemplateRequired>
            <ns:ConditionalParentLabel>Paymentmethod</ns:ConditionalParentLabel>
                <ns:ConditionalParentValue>No</ns:ConditionalParentValue>
                <ns:SharedTab>false</ns:SharedTab>
            <ns:RequireInitialOnSharedTabChange>false</ns:RequireInitialOnSharedTabChange>
                <ns:ConcealValueOnDocument>false</ns:ConcealValueOnDocument>
                <ns:Font>LucidaConsole</ns:Font>
                <ns:Bold>false</ns:Bold>
                <ns:Italic>false</ns:Italic>
                <ns:Underline>false</ns:Underline>
                <ns:FontColor>Black</ns:FontColor>
                <ns:FontSize>Size9</ns:FontSize>
                <ns:IncludeNoteInEmail>false</ns:IncludeNoteInEmail>
                <ns:IsPaymentAmount>false</ns:IsPaymentAmount>
                <ns:RoundDecimalPlaces>0</ns:RoundDecimalPlaces>
                <ns:MaxLength>4000</ns:MaxLength>
                <ns:RequireAll>false</ns:RequireAll>
             </ns:Tab>
            </ns:Tabs>
            <ns:Subject>Email Subject Radio</ns:Subject>
            <ns:EmailBlurb>Email Blurb Radio</ns:EmailBlurb>
         </ns:Envelope>
      </ns:CreateAndSendEnvelope>
   </soapenv:Body>
</soapenv:Envelope>

付款实体:

    @ManyToMany
    @JoinTable(name="BILL_PAYMENTS",
        joinColumns=
        @JoinColumn(name="payments_id", referencedColumnName="id"),
        inverseJoinColumns=
        @JoinColumn(name="bills_id", referencedColumnName="id")
)
private List<Payment> payments = new ArrayList<>(0);

这是我的BillService文件:

@ManyToMany(mappedBy = "payments", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private List<Bill> bills = new ArrayList<>(0);

不知道发生了什么,我已经检查过stackoverflow上的类似问题,但它没有任何帮助。 谢谢c:

1 个答案:

答案 0 :(得分:1)

您正在尝试保留与瞬态对象相关的对象,但您的付款未受管理,因此当您尝试保存帐单时,您将获得此例外。

解决方案1:您需要保存每笔付款并添加托管对象(从保存方法返回的对象)。

解决方案2:您可以将付款级联,以便在帐单持续存在时付款也会保留

$password