I'm a newbie in Hibernate. I have this simple method in a Dao implementation class:
Code:
public long inclui(Produto umProduto) throws AplicacaoException
{ try
{ EntityManager em = JPAUtil.getEntityManager();
em.persist(umProduto);
return umProduto.getId();
}
catch (ConstraintViolationException e)
{ throw new AplicacaoException("Produto com Nome duplicado");
//product with a duplicated name
}
catch(RuntimeException e)
{ throw new InfraestruturaException(e);
}
}
The entity PRODUTO (Product) is mapped to a table PRODUTO (PRODUCT) that has 5 columns: DATA_CADASTRO (creation date) , DATA_VENDA (sold date), LANCE_MINIMO (lower price), NOME (name) e ID . Id is auto-generated (SEQUENCE) and the rest are basic columns.
In Database, the PRODUTO TABLE, has a UNIQUE KEY constraint on NOME Column. When I try to insert a product with a name that already exists in table, I receive the following error:
29/10/2009 16:32:21-org.hibernate.util.JDBCExceptionReporter logExceptions
ERROR: ORA-00001: restrição exclusiva (JPA1.UQ_PRODUTO_NOME) violada
29/10/2009 16:32:21-org.hibernate.util.JDBCExceptionReporter logExceptions
ERROR: ORA-00001: restrição exclusiva (JPA1.UQ_PRODUTO_NOME) violada
29/10/2009 16:32:21-org.hibernate.event.def.AbstractFlushingEventListener performExecutions
ERROR: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
at exercicio.JPAUtil.commitTransaction(JPAUtil.java:64)
at exercicio.ProdutoAppService.inclui(ProdutoAppService.java:17)
at exercicio.Principal.main(Principal.java:40)
Caused by: java.sql.BatchUpdateException: ORA-00001: restrição exclusiva (JPA1.UQ_PRODUTO_NOME) violada
at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:441)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3377)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 11 more
Vai efetuar rollback de transacao
Vai efetuar rollback de transacao
Exception in thread "main" exercicio.InfraestruturaException: javax.persistence.RollbackException: Error while commiting the transaction
at exercicio.JPAUtil.commitTransaction(JPAUtil.java:76)
at exercicio.ProdutoAppService.inclui(ProdutoAppService.java:17)
at exercicio.Principal.main(Principal.java:40)
Caused by: javax.persistence.RollbackException: Error while commiting the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
at exercicio.JPAUtil.commitTransaction(JPAUtil.java:64)
... 2 more
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
... 3 more
Caused by: java.sql.BatchUpdateException: ORA-00001: restrição exclusiva (JPA1.UQ_PRODUTO_NOME) violada
at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:441)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3377)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 11 more
The sql generated are:
Code:
Hibernate:
select
SEQ_PRODUTO.nextval
from
dual
Hibernate:
insert
into
PRODUTO
(DATA_CADASTRO, DATA_VENDA, LANCE_MINIMO, nome, ID)
values
(?, ?, ?, ?, ?)
How can I catch the exception?
Tks in advance.