English 中文(简体)
Java Bean Validation (JSR303) constraints involving relationship between several bean properties
原标题:

Say I have the following simple java bean:

class MyBean {
   private Date startDate;
   private Date endDate;
   //setter, getters etc...
}

Is there a mechanism in JSR 303 to create a custom validator that validates the constraint that startDate must be before endDate?

It seems to me to be a common use case, yet I cannot find any examples of this kind of multiple property relationsship constraint.

最佳回答

I can think of a few things to try.

You could create a Constraint with a target of the type itself with an appropriate validator:

@ValidateDateRange(start="startDate", end="endDate")
public class MyBean {

You could encapsulate a date range in a type and validate that:

public class DateRange {    
  private long start;
  private long end;

  public void setStart(Date start) {
    this.start = start.getTime();
  }

  // etc.

You could add a simple property that performs the check:

public class MyBean {
  private Date startDate;
  private Date endDate;

  @AssertTrue public boolean isValidRange() {
    // TODO: null checks
    return endDate.compareTo(startDate) >= 0;
  }
问题回答

If you re using Hibernate Validator in version 4.1 or later you can use the @ScriptAssert constraint together with a scripting or expression language to express this kind of constraint. Using JavaScript your example would look like this:

 @ScriptAssert(lang = "javascript", script = "_this.startDate.before(_this.endDate)")
 public class CalendarEvent {

      private Date startDate;

      private Date endDate;

      //...
 } 

You can get an even more compact syntax by creating a custom constraint for the script language of your choice:

@JexlAssert("_.startDate < _.endDate")
public class CalendarEvent {

    private Date startDate;

    private Date endDate;

    //...
}

The answer is a class level validator. You can define custom constraints which you can place on entity class. At validation time you get passed the whole instance into the isValid() method. You can then compare your two dates against each other. See also the Hibernate Validator documentation.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签