春季启动-在某些情况下禁用验证注释

时间:2018-11-16 15:29:13

标签: spring hibernate validation spring-boot annotations

我有带有两个自定义验证注释@FieldMatch@CategoryExists的类别实体。这是代码:

@Entity
@Table(name = "Category")
@FieldMatch(first = "defaultSrcLanguage", second = 
"defaultTargetLanguage", message = "Languages must be different")
@CategoryExists(category="name", message="Category already exists")
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Size(min=2, message="Enter at least 2 characters...")
@Column(name = "name")
private String name;

@OneToOne
@NotNull
private Language defaultSrcLanguage;

@OneToOne
@NotNull
private Language defaultTargetLanguage;

private String defaultTargetSide;

private int defaultCountdownDuration;

// getters and setters

@CategoryExists检查数据库中是否已存在具有提供名称的类别。如果存在,则显示错误消息:“类别已存在”。在创建新类别时需要。但是在更新类别(即“ Test5”)期间,我不希望总是更改类别名称,而希望更改其他字段,例如defaultSrcLanguage。顺便说一句,我将收到相同的错误消息“类别已存在”。是否可以禁用注释@CategoryExist进行更新?也许其他解决方案?

0 个答案:

没有答案