我有以下课程:
@Entity
@Table(name = "EMPLOYEES")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long employeeId;
@OneToMany
private List<EmployeeProjectAssignment> projectListAssignmentList;
private String firstName;
private String lastName;
private String lastChangedBy = "INITIAL";
@Version
@Column(name = "optlock", columnDefinition = "integer DEFAULT 0", nullable = false)
private Long version = 0L;
现在我意识到,我的乐观锁定无法正常工作。
我尝试如下:
我在Firefox和Crome中打开了程序,并以两个不同的用户身份登录。然后,我在两个浏览器中打开了ID = 1的员工。两者都向我显示Version = 0。此后,我在一个浏览器中将名称更新为Rickardss并保存,因此版本号nummer自动升为1。现在,如果我尝试在第二个浏览器中更新数据,将会得到一个错误。但是它可以工作,并且版本号从显示的0跳到2。
因此,我研究了stacktrace并发现了以下
2018-07-25 10:58:51.939 DEBUG 9508 --- [nio-9000-exec-8] org.hibernate.SQL : select employee0_.employee_id as employee1_1_, employee0_.first_name as first_na2_1_, employee0_.last_changed_by as last_cha3_1_, employee0_.last_name as last_nam4_1_, employee0_.optlock as optlock5_1_ from employees employee0_ where employee0_.employee_id=?
2018-07-25 10:58:51.940 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
2018-07-25 10:58:51.943 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicExtractor : extracted value ([employee1_1_] : [BIGINT]) - [1]
2018-07-25 10:58:51.943 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicExtractor : extracted value ([first_na2_1_] : [VARCHAR]) - [Rickardss]
2018-07-25 10:58:51.943 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicExtractor : extracted value ([last_cha3_1_] : [VARCHAR]) - [laubem]
2018-07-25 10:58:51.943 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicExtractor : extracted value ([last_nam4_1_] : [VARCHAR]) - [Stark]
2018-07-25 10:58:51.943 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicExtractor : extracted value ([optlock5_1_] : [BIGINT]) - [2]
2018-07-25 10:58:51.943 TRACE 9508 --- [nio-9000-exec-8] org.hibernate.type.CollectionType : Created collection wrapper: [de.msg.Mep.domain.Employee.projectListAssignmentList#1]
2018-07-25 10:58:51.945 DEBUG 9508 --- [nio-9000-exec-8] org.hibernate.SQL : update employees set first_name=?, last_changed_by=?, last_name=?, optlock=? where employee_id=? and optlock=?
2018-07-25 10:58:51.946 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [Rickardqqqaa]
2018-07-25 10:58:51.946 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [egertm]
2018-07-25 10:58:51.946 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [VARCHAR] - [Stark]
2018-07-25 10:58:51.946 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [BIGINT] - [3]
2018-07-25 10:58:51.946 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [BIGINT] - [1]
2018-07-25 10:58:51.946 TRACE 9508 --- [nio-9000-exec-8] o.h.type.descriptor.sql.BasicBinder : binding parameter [6] as [BIGINT] - [2]
这意味着程序将在后台获取数据的新版本(Ricardss)并对其进行更新。但这不是乐观锁定应该如何工作的方式。
我的错误在哪里?
感谢期待
Matthias