-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Optimistic Locking Problem
PostPosted: Fri Aug 24, 2007 10:22 am 
Newbie

Joined: Wed Aug 22, 2007 8:44 am
Posts: 5
Location: Brussells
Hi,

I am trying to use hibernate optimistic locking on one of my entities. I have a field with JPA annotations as such:

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


I have wiped the database to start afresh but something odd happens. I have created 3 new entities. When I modify the first hibernate runs update statements against the first And the second entity, hence version numbers increase for both. I cannot think of any reason why hibernate should try and update the second entity.

When I modify the second created entity, it works fine and only the version number for this one is incremented.

Modifying the third entity runs updates (and increments versions) for inself and the second entity, which is also unexpected (and unwanted).

Would appreciate if anybody has experienced anything similar and can offer any exoplantion.

(I am using JPA with hibernate underneath)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 25, 2007 1:09 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
You might get more help if you post a code example


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 3:36 am 
Newbie

Joined: Wed Aug 22, 2007 8:44 am
Posts: 5
Location: Brussells
Well there isn't any interesting code really. I should have called the Subject: Problem with Hibernate Automatic Versioning. Hibernate increases the version number of the entity I modify and some others (not all). My service layer method that does the save is as follows:

@Transactional
public Long saveApplication(Application application) {

//Determine whether a new application or an existing application
Long applicationId = application.getId();
if (applicationId == null) {
//Persist new application
application.setStatus(Constants.APPLICATION_STATUS_INCOMPLETE);
populateApplicationFields(application);
entityManager.persist(application);
applicationId = application.getId();
} else {
//Update existing application
entityManager.merge(application);
}

return applicationId;
}

Before this is called, all applications are loaded using method:

@Transactional (readOnly = true)
public List<Application> loadApplications() {
Query query = entityManager.createQuery("from org.ec.tentec.domain.Application as application where application.status != 'CLOSED' and application.status != 'DELETED' order by application.projectCode");
List<Application> resultsList = query.getResultList();

return resultsList;
}

Then one is loaded by id for modification using method:

@Transactional (readOnly = true)
public Application loadApplicationById(Long applicationId) {

Application application = entityManager.find(Application.class, applicationId);
return application;
}

Any ideas appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 10:31 am 
Newbie

Joined: Wed Aug 22, 2007 8:44 am
Posts: 5
Location: Brussells
I think I have solved this now. It looks like my dubious implementations of equals method thoughtout my domain classes (inclusion of primary keys used by hibernate) was causing odd automatic versioning behaviour!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.