我是一名 Play/Hibernate 新手,试图扩展我继承的 Play 1.2.3 应用程序,该应用程序解析具有不可靠数据的文件内容并尝试将其持久保存到数据库中。
当应用程序在将其内容持久保存到数据库时出现问题时,我需要一种在数据库中进行标记的方法。在应用程序甚至没有捕获 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 对象并为其赋予与前一个相同的值,包括先前获取的 Hibernate ID(和调用merge()
),但这导致:
PersistenceException occured : org.hibernate.PersistentObjectException:
detached entity passed to persist: models.MyObject
如果我不给它之前获取的 Hibernate 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
我认为上述方法是最好的方法,因为这会使我无法级联到所有我不想这样做的子对象(并且会再次失败)。
查看文档,我没有想法。希望有人可以帮助我!我想做的只是将状态代码写入父对象(不是所有有问题的子对象)的数据库行,以便我可以标记此处存在持久性失败。
谢谢!