我有一个简单的循环,它将同一事务中的一些实体插入到DB2数据库中。问题是,当提交事务并且插入因ID重复而失败时,我从驱动程序获得的异常是一条明确的消息,指示insert语句失败的表,但我需要查找哪个实体。有没有办法翻译从数据库中抛出的异常?
方法是:
public final void saveAll(List<Entity> entities)
{
StatelessSession session = getSaveQuerySession();
Transaction tr = session.beginTransaction();
try
{
for(Entity entity : entities)
session.insert(entity);
tr.commit();
}
catch (Exception e)
{
e.printStackTrace();
tr.rollback();
}
finally{
session.close();
}
}
我得到的例外是:
org.hibernate.exception.ConstraintViolationException: could not insert: [com.MyEntity]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2438)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2858)
编辑
内部例外是:
java.sql.SQLIntegrityConstraintViolationException: [SQL0803] Duplicate key value specified.
Cause . . . . . : A unique index or unique constraint *N in *N exists over one or more columns of table MYENTITY in MYSCHEMA.
The operation cannot be performed because one or more values would have produced a duplicate key in the unique index or constraint. Recovery . . . :
Change the statement so that duplicate keys are not produced.
For information on what rows contain the duplicate key values,
look at the previously listed messages in the job log (DSPJOBLOG command) or press F10 (Display messages in job log) on this display.