-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Catch Exception - JPA
PostPosted: Thu Oct 29, 2009 2:47 pm 
Newbie

Joined: Wed Oct 28, 2009 2:45 pm
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Catch Exception - JPA
PostPosted: Tue Nov 03, 2009 2:16 pm 
Newbie

Joined: Wed Oct 28, 2009 2:45 pm
Posts: 2
It is necessary to put a flush method to force the syncronization with DB.

OD


Code:
   public long inclui(Produto umProduto) throws AplicacaoException
   {   try
      {   EntityManager em = JPAUtil.getEntityManager();

         em.persist(umProduto);

         em.flush();
         
         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);
      }
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.