Validation of a date with Hibernate

24,630

Solution 1

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.

Solution 2

Now, in the updated new validator version i.e.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.4.Final</version>
</dependency>

via

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.0.Final</version>
</dependency>

we have @FutureOrPresent and many other helpful annotations that you can use.

Share:
24,630
Kendall H.
Author by

Kendall H.

Updated on November 16, 2020

Comments

  • Kendall H.
    Kendall H. over 3 years

    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.

  • Kendall H.
    Kendall H. over 7 years
    Sorry for the late response Sir. This are the codes for startDate: pastie.org/10958151 I hope you can guide me. Thank you.
  • thatguy
    thatguy over 7 years
    If you can only allow the current timestamp, why do you have a setter method? Do you use Hibernate or JPA?
  • Kendall H.
    Kendall H. over 7 years
    Sorry, I edited my question Sir. It should accept current & future date. I use hibernate.
  • Kendall H.
    Kendall H. over 7 years
    Thank you for your help Sir! :) I will try this and update you. :)
  • Kendall H.
    Kendall H. over 7 years
    I tried your code Sir. I created a PresentOrFuture.java annotation and PresentOrFutureValidator.java class and added @PresentOrFuture but whenever I try to run the program I get this error: "The web application [/hms] registered the JDBC driver [org.hsqldb.jdbc.JDBCDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered."
  • Kendall H.
    Kendall H. over 7 years
    Still getting the same error when I run it in the command prompt Sir. Here's what happens when I "mvn tomcat7:run" it: pastie.org/private/cyemsdl01qerwy2dfvnpjw
  • thatguy
    thatguy over 7 years
    Do you have compile errors or warnings on this? I have changed the target annotation to @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }), I do not think this is the problem, but you can try. Which Java version do you use?
  • Kendall H.
    Kendall H. over 7 years
    I don't Sir. I tried it but I still get the same error Sir. Java 8.
  • thatguy
    thatguy over 7 years
    Sadly, I do not have the environment to test it myself, but since you use Java 8, I probably have another solution for you. The error ASM ClassReader failed to parse class file can come from using Java 8 in old versions of Spring, see here or here. Now, compile your code with Java 7 or at last upgrade to Spring 4. Update me, Madame.
  • Kendall H.
    Kendall H. over 7 years
    Sorry Sir, I was wrong the Java Version that I'm using is 1.6 and not 8. Other solution you suggest? Thank you for your patience Sir.
  • thatguy
    thatguy over 7 years
    What Spring version do you use?
  • Kendall H.
    Kendall H. over 7 years
    3.2.8.RELEASE Sir.
  • thatguy
    thatguy over 7 years
    As already stated here by ItayK, this may be a bug concerning ASM in Spring 3.2.8. The best you can do is upgrade to Spring 4.