简体   繁体   English

如何在 Spring Boot 项目中禁用 Hibernate 验证

[英]How to disable Hibernate validation in a Spring Boot project

I have a spring boot project that has a CrudRepository, an Entity and a Controller.我有一个 Spring Boot 项目,它有一个 CrudRepository、一个实体和一个控制器。 I am basically trying to persist an entity based on the data passed to the Controller.我基本上是在尝试根据传递给控制器​​的数据来持久化一个实体。

To do this, I am using spring-boot-starter-jpa .为此,我使用spring-boot-starter-jpa My Entity is annotated with JSR-303 annotations, which are checked in the controller before the data gets passed to the CrudRepository for persistence.我的实体使用 JSR-303 批注进行批注,将数据传递到 CrudRepository 以进行持久化之前,这些批注会在控制器中进行检查。

Controller method:控制器方法:

@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 cause of my issues is the UserUnique annotation.我的问题的原因是UserUnique注释。 Its validator looks like this:它的验证器看起来像这样:

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, the validation is getting run twice.似乎正在发生的是,验证正在运行两次。 Once in the controller via the @Valid annotation, and once when Hibernate tries to persist the object.一次通过@Valid注释进入控制器,一次当 Hibernate 尝试持久化对象时。 However, when Hibernate tries to persist the object, it throws:但是,当 Hibernate 尝试持久化对象时,它会抛出:

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

This seems to be because its not spring-aware and cant inject the dependency into the constructor.这似乎是因为它没有弹簧意识并且无法将依赖项注入到构造函数中。 So, what I want to do is disable Hibernate validation completely (as its redundant and already happening in the controller).所以,我想要做的是完全禁用 Hibernate 验证(因为它是冗余的并且已经在控制器中发生)。

There seems to be a property called javax.persistence.validation.mode which you can set to none .似乎有一个名为javax.persistence.validation.mode的属性,您可以将其设置为none However, I cant for the life of me figure out where to set it in a code-based configuration.但是,我一生都无法弄清楚在基于代码的配置中将它设置在哪里。

I realise there are questions like JSR-303 dependency injection and Hibernate but these are all using xml config and manually configuring parts of the persistence layer.我意识到有像JSR-303 依赖注入和 Hibernate 这样的问题,但这些都是使用 xml 配置和手动配置持久层的部分。

What I want to do is "post-configure" the required parts of the persistence layer that Spring Boot creates for me because if I define my own then I am no longer leveraging Spring Boot's auto configuration.我想要做的是“后配置”Spring Boot 为我创建的持久层的必需部分,因为如果我定义自己的,那么我就不再利用 Spring Boot 的自动配置。 Can anyone help me determine if 1) this is possible and 2) which parts do I need to configure and how?任何人都可以帮助我确定 1) 这是否可行以及 2) 我需要配置哪些部分以及如何配置?

Thanks!谢谢!

As [M.作为 [M. Deinum] mentioned in a comment on my original post, the solution is to set: Deinum] 在我原帖的评论中提到,解决方法是设置:

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

In the application.properties file.application.properties文件中。

Additionally, this behaviour is described here (its easy to miss because no example is provided).此外, 这里描述了这种行为(很容易错过,因为没有提供示例)。

@Erin Drummond's Answer is for database entity validation (individual records) @Erin Drummond 的答案是用于数据库实体验证(单个记录)

But if someone ran into a problem with schema validation below property works well.但是,如果有人遇到以下属性的模式验证问题,则效果很好。

# Hibernate ddl auto (create, create-drop, validate, update, none)
spring.jpa.hibernate.ddl-auto=none

Actually, in spring boot 2.x.实际上,在 spring boot 2.x 中。 it is:这是:

spring.jpa.hibernate.ddl-auto: none

for spring boot unless you add validation dependency the spring validation would not kick in.对于 spring 引导,除非您添加验证依赖项,否则 spring 验证不会启动。

 <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-validation</artifactId> 
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM