[bv-dev] Improved java.time support follow-up

Gunnar Morling gunnar at hibernate.org
Tue Jan 10 03:38:30 EST 2017


Hi Michael,

> A more interesting use case that remains unsupported, of which the previous is kind
> of a specialization is: I have a startDate and an endDate and there is no simple way to
> express that endDate must be greater or equal than startDate. That would apply to
> any Comparable, by the way

It's an interesting request, but I'm not sure whether it pulls its
weight. We'd probably end up with something looking like that:

    @Compare(
        member1 = "dateOfEntry",
        comparison = ComparisonType.LESS_THAN,
        member2 = "dateOfCancelation"
    )
    public  class Customer {
        public LocalDateTime dateOfEntry;
        public LocalDateTime dateOfCancelation;
    }

I find it doesn't read overly well. It feels a bit too much like
"programming in annotations" to me. It also raises the question how to
retrieve the values for member1 and member2, getter or field?

But there are simple ways for implementing your requirement. Using
@AssertTrue, you can do it by writing actual, safe Java code:

    public  class Customer {
        public LocalDateTime dateOfEntry;
        public LocalDateTime dateOfCancelation;

        @AssertTrue
        public boolean isEntryAndCancelationDatesConsistent() {
            return dateOfEntry.compareTo( dateOfCancelation ) < 0;
        }
    }

An alternative is the RI's @ScriptAssert. It even discusses your very
example in the docs:

    @ScriptAssert(lang = "jexl", script = "_.startDate < _.endDate",
alias = "_")
    public class CalendarEvent {

        private Date startDate;
        private Date endDate;

        //...
    }

Would either approach help to address your requirements?

--Gunnar


More information about the beanvalidation-dev mailing list