1, in order to solve the problems caused by concurrency, we usually declare a version management field on the entity class, and introduce the @version annotation, which simply realizes the optimistic locking function;
Java code
- @Version
- Private Long version;
2, when the Action form submission, Hibernate will be based on the type of operation (if it is new, the default is to generate a version 0 version number,
Each subsequent modification will be based on the existing version plus 1) to maintain the version number.
Application Scenarios:
If two people (A, b) at the same time to the same entity to make changes, a first submitted, b after the submission, then B will report the error. (Because A has been modified, the version number submitted by B is lower than the version number in the database and therefore cannot be updated).
Precautions:
Use the versioning field to be aware that the version number is to be committed when modified, or hibernate throws a null pointer exception because the current version cannot be referenced due to maintenance of the version number.
Hibernate exception Throw Location:
Java code
- Org.hibernate.type.LongType
- @SuppressWarnings ({ "unnecessaryboxing", "Unnecessaryunboxing"})
- Public long Next (long, Sessionimplementor session) {
- return long.valueof (Current.longvalue () + 1);
- }
Exception information:
Critical: Servlet.service () for Servlet Springmvc threw exception
Java.lang.NullPointerException
At Org.hibernate.type.LongType.next (longtype.java:76)
At Org.hibernate.type.LongType.next (longtype.java:40)
At Org.hibernate.engine.Versioning.increment (versioning.java:131)
At Org.hibernate.event.def.DefaultFlushEntityEventListener.getNextVersion (Defaultflushentityeventlistener.java : 406)
At Org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate (Defaultflushentityeventlistener.java : 296)
At Org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity (Defaultflushentityeventlistener.java : 155)
At Org.hibernate.event.def.AbstractFlushingEventListener.flushEntities (abstractflushingeventlistener.java:219)
At Org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions ( ABSTRACTFLUSHINGEVENTLISTENER.JAVA:99)
At Org.hibernate.event.def.DefaultFlushEventListener.onFlush (defaultflusheventlistener.java:50)
Java.lang.NullPointerException at Org.hibernate.type.LongType.next (Longtype.java