0

I have Category Entity with two custom validation annotations @FieldMatch and @CategoryExists. This is the code:

@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 checks whether category, with provided name, already exists in database. If exists, the error message is displayed: "Category already exists". It's desired during creating new category. But during updating category i.e. "Test5" I don't want always to change category name, but other fields such as defaultSrcLanguage. Btw, I will receive same error message "Category already exists". It's possible to disable annotation @CategoryExist for updating? Maybe other solution?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
outlaw1988
  • 41
  • 7
  • Sure, there is a way. That's what validation groups are for. This answer may probably help https://stackoverflow.com/a/43984952/1654233. – yegodm Nov 16 '18 at 16:17
  • Thank you! I found good tutorial [here](http://learningprogramming.net/java/spring-mvc/validation-groups-in-spring-mvc/) It works. – outlaw1988 Nov 16 '18 at 17:58

0 Answers0