I have the following:
public class Member {
@Id
@Column
private String id;
@Column
private String location;
// setters/getters
}
I have to implement the following business rules:
Set Location:
- Get member from DB based on id.
- If location is null, assign a value to location and save, otherwise do nothing.
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();
}
Remove Location:
- Get member from DB based on id.
- If location has a non-null value and current value equals parameter value, set location to null and update, otherwise do nothing
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);
}
}
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.
Basically, the first update should win and the second should fail.
Does it suffice to add a version to the Member class
@Version
@Column(name="version")
private Long version;
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.
Thanks.