2013-03-28 9 views
0

我是一個Play/Hibernate新手,試圖擴展一個Play 1.2.3應用程序我繼承瞭解析文件內容的不可靠數據並試圖保留它到數據庫。使用Play框架,在與數據庫相關的異常後堅持Hibernate對象

我需要一種方式來標記數據庫中的應用程序何時有問題持久其內容到數據庫。在應用程序甚至沒有捕捉到SQL異常並且徹底崩潰之前。所以,我把所有的東西都包裝在try-catch塊中,希望在catch塊中能夠優雅地處理錯誤並在數據庫中記錄失敗。

粗略地說,這是我更新的代碼:

try { 
    MyObject parent = new MyObject(); 
    // Persist the parent object and all of its child objects. The SQL errors are 
    // occurring on the child objects. 
    parent.save(); 
    // Do a bit more processing 
    parent.status=1; 
    parent.save(); 
} 
catch { 
    // This is what I'm trying to do -- set the failure status 
    // in the database to look at later 
    parent.status = 0; 
    parent.save(); 
} 

myObject的繼承:

package models; 
import javax.persistence.*; 

import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.MappedSuperclass; 

@MappedSuperclass 
public class MyObjectModel extends play.db.jpa.GenericModel { .. } 

如上所寫的代碼拋出:

ERROR: current transaction is aborted, commands ignored until end of transaction block 
ERROR ~ Could not synchronize database state with session 

我打過電話parent.refresh()之前save(),但導致:

play.exceptions.JavaExecutionException: org.hibernate.exception.GenericJDBCException: 
could not load an entity: [models.MyObject#465655] 

我試圖創建一個新的MyObject對象,並給它相同的值與前一個,包括以前牽強休眠ID(和呼叫merge()),而導致:

PersistenceException occured : org.hibernate.PersistentObjectException: 
detached entity passed to persist: models.MyObject 

如果我不給它先前獲取的休眠ID,它拋出:

ERROR: current transaction is aborted, commands ignored until end of transaction block 
PersistenceException occured : org.hibernate.exception.GenericJDBCException: 
could not get next sequence value 

我認爲以上是最好的方式,因爲這將讓我從級聯到所有子對象, whic我不想這樣做(以及哪些將會變成現實。再次失敗)。

Looking at the documentation,我沒有想法。希望有人能幫助我!我所要做的就是向的數據庫行寫一個狀態代碼父對象(不是所有有問題的孩子),這樣我就可以標記出這裏存在持久性失敗。

謝謝!

回答

0

我想,當你有這樣的異常交易被標記爲只回滾(通過JPA.setRollbackOnly()。和所有你需要做的就是提交當前事務並開始一個新的(通過JPAPlugin.closeTxstartTx)。

+0

哇,太好了!非常感謝。 – TAH 2013-03-28 14:53:12

相關問題