@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;
を定義し、
@PrePersist
protected void onCreateStartDate() {
startDate = new Date();
または更新に
@PreUpdate
protected void onUpdateStartDate() {
startDate = new Date();
UPDAを使用することができます例と例
開始日を現在に固定しないように質問を更新した後、別の方法を実行する必要があります。 hereのように、日付が現在のものか将来のものかをチェックするカスタムバリデーターを作成する必要があります。
は、したがって、あなたはPresentOrFuture.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);
}
}
が次にあなたが設定する必要があります:
@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
でバリデータを定義する必要があり
@NotNull
@PresentOrFuture
@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.DATE)
private Date startDate;
まあ、それはexhausiveだった。私は自分自身でそれをテストしていない、私は今そうする準備がないので、それは動作するはずです。私はそれが助けて欲しい今
注釈 '@ Future'はどのクラス/フレームワークから来ていますか? – Axel
それは私が考えている休眠です –
現在の日付が唯一の有効な値であれば、なぜフィールドを持っているのでしょうか? –