我已经创建了Hibernate“创建”方法:
public PK create(T o) {
try {
getSession().beginTransaction();
PK rez = (PK) getSession().save(o);
getSession().flush();
getSession().getTransaction().commit();
return rez;
}
catch (Exception e){
e.printStackTrace();
getSession().getTransaction().rollback();
return null;
}
}
当发生某些异常时(例如,DB中的唯一约束),则执行catch块。出现一个错误后,我无法创建另一行...我对旧错误有异常。例如,我创建用户“test”,然后我尝试添加具有相同名称“test”的用户,并且我在日志文件中遇到hibernate错误。然后我尝试添加新的唯一用户,使用新的用户名“test2”,但仍然得到关于“test”用户的旧错误。问题出在哪儿?
所以,真实的例子:
我添加了testuser1@ttt.org,之后,我尝试再次添加此用户(并获得异常(下方))...现在我尝试添加用户" testuser3@ttt.org"但是我再次收到关于testuser1@ttt.org的错误:
[2014-03-23T17:35:17.767+0200] [glassfish 4.0] [WARN] [] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] [tid: _ThreadID=22 _ThreadName=http-listener-1(5)] [timeMillis: 1395588917767] [levelValue: 900] [[
SQL Error: 0, SQLState: 23505]]
[2014-03-23T17:35:17.767+0200] [glassfish 4.0] [ERROR] [] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] [tid: _ThreadID=22 _ThreadName=http-listener-1(5)] [timeMillis: 1395588917767] [levelValue: 1000] [[
ERROR: duplicate key value violates unique constraint "unique_Username"
Detail: Key (username)=(testuser1@ttt.org ) already exists.]]
[2014-03-23T17:35:17.767+0200] [glassfish 4.0] [SEVERE] [] [] [tid: _ThreadID=22 _ThreadName=Thread-4] [timeMillis: 1395588917767] [levelValue: 1000] [[
org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3067)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3509)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:286)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
at org.test.majas.dao.GenericDaoImpl.create(GenericDaoImpl.java:29)
at org.test.majas.beans.TestBean.addUser(TestBean.java:80)
.....
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "unique_Username"
Detail: Key (username)=(testuser1@ttt.org ) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:363)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
... 63 more]]
感谢。
答案 0 :(得分:0)
所以,问题出在Session ......每次出错后我都要创建新的Session ......