Java – how to disable hibernate authentication in spring boot projects

I have a spring guided project with a crudrepository, an entity and a controller I basically try to persist an entity based on the data passed to the controller

To do this, I use spring boot starter JPA My entity is annotated with jsr-303 annotations, which are checked in the controller before passing data to the crudrepository for persistence

Controller mode:

@RequestMapping(value = "users",method = { RequestMethod.POST })
public SuccessfulResponse<User> addUser(@Valid @RequestBody User user,BindingResult validation) {
    if (validation.hasErrors()) {
        throw new ValidationException(validation);
    }
    User saved = this.users.save(user);
    return new SuccessfulResponse<User>(saved);
}

Entity:

@Entity /* JPA */
public class User {

   @Id /* JPA */
   @Column(name="email_address",nullable=false,length=255) /* JPA */
   @UserUnique
   private String emailAddress;

}

The reason for my problem is the userunique annotation Its validators are as follows:

public class UserUniqueValidator implements ConstraintValidator<UserUnique,String> {

   private UserRepository users;

   @Autowired
   public UserUniqueValidator(UserRepository users) {
       this.users = users;
   }

   @Override
   public void initialize(UserUnique annotation) {}

   @Override
   public boolean isValid(String value,ConstraintValidatorContext context) {
       return users.findOne(value) == null;
   }
}

What seems to be happening is that validation is running twice Once the @ valid annotation is in the controller, and once hibernate attempts to persist the object However, when hibernate tries to keep the object, it throws:

javax.validation.ValidationException: HV000064: Unable to instantiate ConstraintValidator: class test.UserUniqueValidator`

This seems to be because it is not spring aware and cannot inject dependencies into constructors Therefore, what I want to do is to disable hibernate authentication completely (as its redundancy and has occurred in the controller)

There seems to be one called javax persistence. validation. Mode, which you can set to none However, I can't find a place to set it in code - based configuration for my life

I realize that there are some problems, such as jsr-303 dependency injection and hibernate, but these are persistence layers using XML configuration and manual configuration

What I want to do is "post configure" the part required by the persistence layer created by spring boot for me, because if I define my own, I will no longer use the automatic configuration of spring boot Anyone can help me determine whether 1) this is possible and 2) which parts I need to configure and how?

thank you!

Solution

As [M. deinum] mentioned in his comments on my original post, the solution is to set:

spring.jpa.properties.javax.persistence.validation.mode=none

In application Properties file

In addition, this behavior is described here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>