Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate: update field in entity only if specific field is null
    text
    copied!<p>I have the following:</p> <pre><code>public class Member { @Id @Column private String id; @Column private String location; // setters/getters } </code></pre> <p>I have to implement the following business rules:</p> <p>Set Location:</p> <ol> <li>Get member from DB based on id.</li> <li>If location is null, assign a value to location and save, otherwise do nothing.</li> </ol> <pre><code> public void setLocation(String id, String location){ Member m = memberDao.find(id); // assume m is not null if(null == m.getLocation()){ m.setLocation(location); memeberDao.saveOrUpdate(m); } else throw new MemberAlreadyHasALocationException(); } </code></pre> <p>Remove Location:</p> <ol> <li>Get member from DB based on id.</li> <li>If location has a non-null value and current value equals parameter value, set location to null and update, otherwise do nothing</li> </ol> <pre><code> public void removeLocation(String id, String location){ Member m = memberDao.find(id); if(m.getLocation() != null && m.getLocation.equalsIgnoreCase(location)){ m.setLocation(null); memeberDao.saveOrUpdate(m); } } </code></pre> <p>I am tasked with handling concurrency issues but I am not sure how to do this in hibernate. More specifically, how can I handle concurrent calls to setLocation for the same ID given that the entity with that ID does not currently have a location.</p> <p>Basically, the first update should win and the second should fail. </p> <p>Does it suffice to add a version to the Member class</p> <pre><code> @Version @Column(name="version") private Long version; </code></pre> <p>However, say that there is a gender field, I would like to be able to update the gender field without caring if the location is being updated by another transaction.</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload