2015-10-05 121 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

我需要對數據庫級別的默認值來解決。插入時,沒有默認值。但成功更新將增加並檢查樂觀鎖定