Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation of a date with Hibernate

We have an existing hotel management system. I was asked to add a date validation in the "Create Accommodation" function in the system. The dialog looks like this:

enter image description here

The "End Date" is already validated as shown in the code below. The @Future annotation in Hibernate ensures that the date is in the future.

@NotNull
@Future
@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.DATE)
private Date endDate;

EDIT

I was asked to add a validation to the "Start date". Only the present or a future date is allowed. I tried to use a @Present annotation, but I guess there is no such thing. Unfortunately, @Future does not accept today's date. I am new to this kind of thing. So I hope someone can help me. Thank you.

like image 941
Kendall H. Avatar asked Nov 29 '22 22:11

Kendall H.


1 Answers

Hibernate

You can use

@CreationTimestamp
@Temporal(TemporalType.DATE)
@Column(name = "create_date")
private Date startDate;

or on update

@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modify_date")
private Date startDate;

Java (JPA)

You can define a field Date startDate; and use

@PrePersist
protected void onCreateStartDate() {
startDate = new Date();

or on update

@PreUpdate
protected void onUpdateStartDate() {
startDate = new Date();

Update and example

After you have updated your question to not fix the start date to the present, you have to do a different approach. You need to write a custom validator to check if a date is now or in the future, like here.

Therefore you can introduce a new annotation in PresentOrFuture.java:

@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PresentOrFutureValidator.class)
@Documented
public @interface PresentOrFuture {
    String message() default "{PresentOrFuture.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

Then you have to define the validator in PresentOrFutureValidator.java:

public class PresentOrFutureValidator
    implements ConstraintValidator<PresentOrFuture, Date> {

    public final void initialize(final PresentOrFuture annotation) {}

    public final boolean isValid(final Date value,
        final ConstraintValidatorContext context) {

        // Only use the date for comparison
        Calendar calendar = Calendar.getInstance(); 
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Date today = calendar.getTime();

        // Your date must be after today or today (== not before today)
        return !value.before(today) || value.after(today);

    }
}

Then you have to set:

@NotNull
@PresentOrFuture
@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.DATE)
private Date startDate;

Well, that was exhausive. I have not tested it myself, since I do not have a set-up to do so now, but it should work. I hope it helps.

like image 162
thatguy Avatar answered Dec 02 '22 11:12

thatguy