用Hibernate验证日期 [英] Validation of a date with Hibernate
问题描述
我们有一个现有的酒店管理系统。我被要求在系统的创建住宿功能中添加日期验证。对话框如下所示:
< img src =https://i.stack.imgur.com/zy7Ce.pngalt =在这里输入图片描述>
结束日期已经验证,如下面的代码所示。 Hibernate中的 @Future
注释可确保日期处于未来状态。
@NotNull
@Future
@DateTimeFormat(pattern =dd / MM / yyyy)
@Temporal(TemporalType.DATE)
private date dateDate;
编辑
<我被要求向开始日期添加验证。只允许现在或未来的日期。我试图使用 @Present
注释,但我想没有这样的事情。不幸的是, @Future
不接受今天的日期。我对这种事情很陌生。所以我希望有人能帮助我。谢谢。
Hibernate
您可以使用
@CreationTimestamp
@Temporal(TemporalType.DATE)
@Column(name = create_date)
私人日期startDate;
或更新中
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name =modify_date)
private日期startDate;
Java(JPA)
您可以定义一个字段 Date startDate;
并使用
@PrePersist
protected void onCreateStartDate(){
startDate = new Date();
或更新中
@PreUpdate
protected void onUpdateStartDate(){
startDate = new Date();
更新和示例
在更新问题后,如果不将开始日期修改为现在,则必须采取其他方法。您需要编写自定义验证程序来检查日期是现在还是将来,例如。
因此,您可以在 PresentOrFuture.java
:
@Target({ElementType.FIELD,ElementType.METHOD,ElementType。 PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PresentOrFutureValidator.class)
@Documented
public @interface PresentOrFuture {
String message()默认{PresentOrFuture.message};
Class<?> [] groups()default {};
Class <?扩展了Payload> [] payload()default {};
}
然后你必须在 PresentOrFutureValidator中定义验证器。 java
:
public class PresentOrFutureValidator
implements ConstraintValidator< PresentOrFuture,Date> {
public final void initialize(final PresentOrFuture annotation){}
public final boolean isValid(final Date,$ b $ final ConstraintValidatorContext context){
//只使用日期进行比较
日历日历= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
今天的日期= calendar.getTime();
//您的日期必须在今天或今天之后(==不在今天之前)
return!value.before(today)|| value.after(今天);
$ b然后你必须设置: / p>
@NotNull
@PresentOrFuture
@DateTimeFormat(pattern =dd / MM / yyyy)
@Temporal(TemporalType.DATE)
private日期startDate;
好的,那是非常详尽的。我没有自己测试过,因为我现在没有这样做的设置,但它应该工作。我希望它有帮助。
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:
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.
解决方案 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.
这篇关于用Hibernate验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!