简体   繁体   English

是否存在可以使用类字段的此类约束(休眠验证程序)?

[英]Is there such constraints (Hibernate Validator) that can use class fields?

I have class: 我有课:

public class QueryFilterDTO {

@NotEmpty
private List<String> senders;

@NotEmpty
private List<String> subjects;

private Date startDate;
private Date endDate;

@NotEmpty
private List<String> countries;

@NotEmpty
private List<String> companySizes;

public QueryFilterDTO() {

}

And I need to do Date validation: startDate mustn't be greater than endDate . 我需要进行日期验证: startDate 不能大于 endDate But I didn't find something like that in HV . 但是我在HV里没有找到这样的东西。

Should I implement it myself? 我应该自己实施吗? ( custom constraints ) 自定义约束

Probably the easiest way is like this: 可能最简单的方法是这样的:

@PrePersist
protected void validateDates() {
  if(startDate.after(endDate){
  //throw a new custom validation exception..
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM