2015-10-05 119 views
0

我有一个代码,我使用envers和它的伟大工程。我在单独的_AUD表上进行审计。但后来,我需要使用乐观锁定。但它不适用于@Audited注释。这里是我的代码休眠@审查冲突@版本

@MappedSuperclass 
@Audited 
public class BaseVO implements IXtrainVO 
{ 
    @Version 
    @Column(name = "Version") 
    private long version; 
} 

@Entity 
@Table(name = "Person") 
@Audited 
public class PersonVO extends BaseVO 
{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "PersonId", unique = true, nullable = false) 
    private Long personId; 

    @Column(name = "Name", length = 80, nullable = false) 
    @NotBlank 
    @Size(max = 80) 
    private String name; 
} 


@Test 
public void testLocking() throws Exception 
{ 
    PersonVO person = new PersonVO(); 
    person.setName("John"); 
    Long personId = personManager.savePerson(person); 
    PersonVO copy1 = personManager.findPerson(personId); 
    PersonVO copy2 = personManager.findPerson(personId); 
    copy1.setName("John1"); 
    personManager.updatePerson(copy1); 
    copy2.setName("John2"); 
    personManager.updatePerson(copy2); 
} 

代码应该引发optimistict锁定错误,但它没有。相反,我得到

Caused by: java.sql.SQLException: Field 'Version' doesn't have a default value.

但是当我删除@Audited注释,乐观的作品,我也得到HibernateOptimisticLockingFailureException这是我期待的。是否知道这两个注释不能很好地相互配合?我也试图把@NotAudited上的版本列,但仍然不工作

回答

0

我可以用讨论Java - JPA - @Version annotation

我需要对数据库级别的默认值来解决。插入时,没有默认值。但成功更新将增加并检查乐观锁定