Guarantee unique number generation with spring data and hibernate -
I am developing a spring MVC webapp with spring data and want to be in hibernation.
I have a unit composed by a boolean field and an integer field.
The boolean field is false at the beginning and the integer field is zero.
When Boolean fields are correct, then I need to assign an integer field that a unique value equal to Max (CEC) +1
To do this, I will write my service method like this:
@ Override public synchronized obz service (obj entry) {if (entry.getbool ()) and entry .getSeq () == tap) {Integer CEC = Obj.Repository.Getleste Sec (); If (seq == faucet) {seq = 1; } Insetsetset (CEC); } Return entry.save (entry); } and in my repository:
@Query (choose from "Max MAX (seq) +1 OB") Integer getLastSeq (); I have put the synchronized keyword in the service method to ensure that only one thread can get a unique maximum number + 1 number at a time. But I'm sure Not that it works
Do you agree correctly?
Am I convinced that this is the guarantee of seq unity?
Thanks Marco
Example of how to start Bean for that purpose Examples.
@ Service public class sequence initiator deployment {@Autowired Private ObjRepository objRepository; @ Owned Personal Services Service; {Override to Public Zero after {@PropertiesSet} {try {integer max = objRepository.getLastSeq (); Service.setLastSeq (max); } Hold (Exception e) {...}} The LastSeq AtomicIntega field will set in your service set.
Comments
Post a Comment