使用 Hibernate 验证日期 [英] Validation of a date with Hibernate

查看:38
本文介绍了使用 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:

结束日期"已经过验证,如下面的代码所示.Hibernate 中的 @Future 注释确保日期在未来.

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;

编辑

我被要求为开始日期"添加验证.只允许现在或将来的日期.我尝试使用 @Present 注释,但我想没有这样的东西.不幸的是,@Future 不接受今天的日期.我对这种事情很陌生.所以我希望有人可以帮助我.谢谢.

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.

推荐答案

休眠

你可以使用

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

或更新时

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

Java (JPA)

你可以定义一个字段Date startDate;并使用

You can define a field Date startDate; and use

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

或更新时

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

更新和示例

在您更新问题以不将开始日期固定为当前之后,您必须采取不同的方法.您需要编写一个自定义验证器来检查日期是现在还是将来,例如

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.

因此你可以在PresentOrFuture.java中引入一个新的注解:

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 {};
}

然后你必须在PresentOrFutureValidator.java中定义验证器:

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);

    }
}

然后你必须设置:

@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.

这篇关于使用 Hibernate 验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆