-->
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: Violation of unique key -> detached entity passed to pers
PostPosted: Thu Oct 18, 2007 8:42 am 
Beginner
Beginner

Joined: Tue Mar 13, 2007 8:26 am
Posts: 25
Hello,

I'm having problems when retrying to insert a new record in database. I do a persist and when the container (CMT) commits the transaction they throw one RollbackException. Then, I catch this exception and try to insert again. But, this exception is throw:

Quote:
org.hibernate.PersistentObjectException: detached entity passed to persist: visualcontrol.model.entity.PedidoVendaItem
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:609)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:601)



The code in EJB is the following:
Code:
    public void incluir(PedidoVenda pedido) throws ValorPropriedadeInvalidaException{
        if (pedido != null) {
            if (pedido.getNaturezaOperacao() == null) {
                pedido.setNaturezaOperacao(naturezaOperacaoPadrao(pedido));
            }
           
            try {
                // Get a new value to avoid violation of unique key
                pedido.setNumero(getProximoNumero());
               
                em.persist(pedido);
                atualizarEstoque(pedido);
            } catch (PersistenceException ex) {
                throw new VisualControlException("Não foi possível incluir o pedido.", ex);
            }
        } else {
            throw new IllegalArgumentException("Pedido não pode ser nulo. Inclusão cancelada.");
        }
    }


The client for this code is:
Code:
                for (int i = 0; i < DBUtils.RETRY.length && !isIncluido(); i++) {
                    try {
                        pedidoFacade.incluir(getPedidoVenda());
                        setIncluido(true);
                    } catch (EJBException ex) {     
                        // Catch the violation of unique key and try again. The method incluir gets a new number
                        if (i < DBUtils.RETRY.length && ex.getCausedByException() instanceof RollbackException) {
                            try {
                                pedidoVenda.setId(0); // Se não commitou zera o id para o hibernate não achar que o objeto ja esta salvo
                                Thread.currentThread().sleep((long) (DBUtils.RETRY[i] * (1.0 + Math.random())));
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            }
                        } else {
                            throw new VisualControlException("Erro ao salvar pedido de venda", ex);
                        }
                    }
                }


Why hibernate is not saving when I try to insert again? The object was not persisted for the first time, because a RollbackException (violation of unique key).

How can I solve this?

Hibernate version:3.2

Name and version of the database you are using: Firebird 1.5.4

_________________
---
Felipe Marin Cypriano


Last edited by fmcypriano on Thu Oct 18, 2007 12:15 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 12:12 pm 
Beginner
Beginner

Joined: Tue Mar 13, 2007 8:26 am
Posts: 25
I'll be more simple (at least I hope).

I've two apps using the same database. The first is the legacy app build with Delphi and the new is a web site that uses Hibernate (EJB3 + JPA) to access the legacy database.

On table PedidoVenda I've a unique key that is generated (by the apps) getting the MAX(NUMBER_PEDIDO) + 1.

The problem is when the legacy app commits (before hibernate) one PedidoVenda with the same number that hibernate is trying to commit. So, when this happen I get from container (Container Managed Transaction) one javax.transactionRollbackException I catch the exception and call the persist again (updating the number to avoid the violation of unique key).

Then when the container try to commit the transaction hibernate thorws one exception: org.hibernate.PersistentObjectException: detached entity passed to persist

Why hibernate don't recognize that the transaction was not committed and consequently the object is not persisted?

How can I do to solve this?

_________________
---
Felipe Marin Cypriano


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.