禁用用于合并/更新的Hibernate验证 [英] Disable Hibernate validation for Merge/Update

查看:62
本文介绍了禁用用于合并/更新的Hibernate验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Order的POJO,如下所示:

I have a POJO called Order as follows:

public class Order {

    @Id
    @GeneratedValue
    @Column(name="id")
    protected int id;

    @NotNull
    @Future
    protected Date desiredProcessingDate;

    protected Date actualProcessingDate;
}

如您所见,必须指定期望的处理日期,并且必须在将来.用户将通过表单提交新订单并指定desiredProcessingDate,该订单正在按预期进行验证.然后通过sessionFactory.getCurrentSession().persist(order);

As you can see, desiredProcessingDate must be specified and it must be in the future. A user will submit a new order through a form and specify a desiredProcessingDate, which is being validated as expected. The order is then persisted to the database via sessionFactory.getCurrentSession().persist(order);

稍后,计划的服务将从数据库中检索订单,进行处理,并使用当前时间戳标记actualProcessingDate.

At a later date a scheduled service will retrieve the order from the database, process it and stamp the actualProcessingDate with the current timestamp.

但是,当此服务调用sessionFactory.getCurrentSession().merge(order);时,由于desiredProcessingDate现在已经过去,验证又重新开始并失败.

However, when this service calls sessionFactory.getCurrentSession().merge(order); the validation kicks in again and fails because desiredProcessingDate is now in the past.

似乎我需要做的是告诉merge(order)操作不进行验证,但是不确定如何执行此操作.

It seems like what I need to do is to tell the merge(order) operation not to validate, but am unsure how to do this.

如果这不可能,那么似乎唯一的办法是编写一个类级自定义验证器,该验证器仅在actualProcessingDate == null时才检查desiredProcessingDate是否在将来,这对于看起来似乎过于复杂一个简单的要求.任何指针将不胜感激.

If this is not possible then it seems like to only way around it is to write a class-level custom validator which only checks that desiredProcessingDate is in the future if actualProcessingDate == null, which seems overly complex for what seems like a simple requirement. Any pointers would be appreciated.

推荐答案

可以使用属性javax.persistence.validation.group.pre-persistjavax.persistence.validation.group.pre-updatejavax.persistence.validation.group.pre-remove配置JPA中的验证行为.这些属性的值是逗号分隔的验证组的完全限定类名的列表,这些验证组将针对每个给定的JPA生命周期事件进行定位,例如:

The validation behaviour in JPA can be configured using the the properties javax.persistence.validation.group.pre-persist, javax.persistence.validation.group.pre-update and javax.persistence.validation.group.pre-remove. The values for these properties is a comma separated list of the fully qualified classnames of the validation groups to target for each of the given JPA life cycle event, eg:

<property name="javax.persistence.validation.group.pre-persist" value="javax.validation.groups.Default,com.acme.PrePersist"/>    
<property name="javax.persistence.validation.group.pre-update" value="javax.validation.groups.Default"/>

作为解决您的问题的方法,是将@Future约束添加到"PrePersist"组中,在这种情况下,仅在持久性而不是更新时才对其进行验证.

As a solution to your problem is to add the @Future constraint to a "PrePersist" group in which case it only gets validated on persist and not on update.

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

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