-->
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.  [ 6 posts ] 
Author Message
 Post subject: org.hibernate.NonUniqueObjectException
PostPosted: Tue Nov 08, 2016 1:58 am 
Newbie

Joined: Tue Nov 08, 2016 1:54 am
Posts: 14
My models have primarykey(autogeneratedID) and a unique code . Unique code is not primarykey.

So, when user sends entity with uniqueCode, I need to check the database whether that particular uniquecode entry is existing or not. If, existing I need to update Else, I need to save.

My code;

Code:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
        public void insertOrUpdate(T entity) {
            try {
                List<T> resultList = null;
                if (entity.getCode() != null) {
                    resultList = criteria.add(Restrictions.eq("code", entity.getCode())).list()
                }
                if (entity.getId() != null) {
                    resultList = criteria.add(Restrictions.eq("code", entity.getId())).list()
                }
                if (resultList != null && !resultList.isEmpty()) {
                    T entity2 = resultList.get(0);
                    entity.setId(entity2.getId());
                    session.merge(entity);  //PROBLEM*****     
                } else {
                session.save(entity);           
                }
            } catch (OptimisticLockException e) {
                session.saveOrUpdate(entity);
            } catch (Exception e) {
                log.error("Save or Update fails, ", e);
            }
        }



If I use, session.saveOrUpdate(entity); instead of session.merge(entity); I get following exception;

Quote:
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : xx


If I use session.merge(entity)` I get
Quote:
avax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)


How can I overcome these exceptions? Im trying to saveOrupdate for a single row. There is no concurrent requests. Only single request.

I create session like; (container managed session)

Code:
public Session getSession() {
    if (session == null || !session.isOpen()) {
        session = getEntityManager().unwrap(Session.class);
    }
    return session;
}


Top
 Profile  
 
 Post subject: Re: org.hibernate.NonUniqueObjectException
PostPosted: Tue Nov 08, 2016 6:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Such application-level verifications are never reliable. What if after you check if there is a conflict record, another transaction adds the same record and commits before you?

Now back to your example, you should always use merge. If you get an OptimisticLockException, it can only mean two things:

1. the entity version has changed
2. you have some issue related to your version mapping

Check the version prior and after the merge. Maybe you are using an old entity snapshot.


Top
 Profile  
 
 Post subject: Re: org.hibernate.NonUniqueObjectException
PostPosted: Tue Nov 08, 2016 7:01 am 
Newbie

Joined: Tue Nov 08, 2016 1:54 am
Posts: 14
Thanks for the info. I see version as "1" in my DB. How can I control version at application level?
Im using Hibernate 5.2.3 final.

I do version like;
Code:
import javax.persistence.Version;


   @Version
   @Column(name = "version")
   private Integer version;


vlad wrote:
Such application-level verifications are never reliable. What if after you check if there is a conflict record, another transaction adds the same record and commits before you?

Now back to your example, you should always use merge. If you get an OptimisticLockException, it can only mean two things:

1. the entity version has changed
2. you have some issue related to your version mapping



Check the version prior and after the merge. Maybe you are using an old entity snapshot.


Top
 Profile  
 
 Post subject: Re: org.hibernate.NonUniqueObjectException
PostPosted: Tue Nov 08, 2016 8:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
I see version as "1" in my DB.


And in the application?


Top
 Profile  
 
 Post subject: Re: org.hibernate.NonUniqueObjectException
PostPosted: Tue Nov 08, 2016 6:23 pm 
Newbie

Joined: Tue Nov 08, 2016 1:54 am
Posts: 14
Code:
ATG entity2 = (ATG) resultList.get(0);
            System.out.println("version.."+entity2.getVersion());  ///prints 1 (entity2, is returned from DB)
            entity.setId(entity2.getId());
            session.merge(entity);  //error occurs, OptimistickException, entity contains version as '0'


But I want to update with entity, which contains latest data. If I don't pass the version number with my entity, when it tries to merge, im getting following exception;
Quote:
javax.ejb.EJBTransactionRolledbackException: Transaction rolled back


How can I overcome?

vlad wrote:
Quote:
I see version as "1" in my DB.


And in the application?


Top
 Profile  
 
 Post subject: Re: org.hibernate.NonUniqueObjectException
PostPosted: Tue Nov 08, 2016 6:55 pm 
Newbie

Joined: Tue Nov 08, 2016 1:54 am
Posts: 14
Thanks for the version hint. I sorted out, by setting version of the entity returned from the DB to my latest entity. Now I can merge.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.