Disable Hibernate validation for Merge/Update

筅森魡賤 提交于 2019-12-12 04:44:28

问题


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

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

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

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

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.

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.


回答1:


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"/>

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.




回答2:


In actuality I would really do this code in a Backing Bean then in the entity itself, I actually would stress that, but it is up to you. This code will be written in the method that persists the Order and of course @Future will be removed completely.

So in the backing bean you would have

//psuedocode
void submitButtonHasBeenPressed(entity){

Date dNow = new Date();
//SimpleDateFormat ft = new SimpleDateFormat("yyyy"); if you need this
if(desiredProcessDateTheUserEnteredInTheField > dNow){
entity.setDesiredProcessingDate(desiredProcessDateTheUserEnteredInTheField);
 }
   else{

   System.out.println("ERROR OCCURED IN VALIDATION");
   FacesContext.getCurrentInstance().addMessage(null,"ERROR OCCURED IN VALIDATION");
   //or show an error Spring style

   }

This way it will never get validated twice by accident



来源:https://stackoverflow.com/questions/25851889/disable-hibernate-validation-for-merge-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!