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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26764532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:14:22  来源:igfitidea点击:

How to disable Hibernate validation in a Spring Boot project

javahibernatejpaspring-bootbean-validation

提问by Erin Drummond

I have a spring boot project that has a CrudRepository, an Entity and a Controller. I am basically trying to persist an entity based on the data passed to the Controller.

我有一个 Spring Boot 项目,它有一个 CrudRepository、一个实体和一个控制器。我基本上是在尝试根据传递给控制器​​的数据来持久化一个实体。

To do this, I am using spring-boot-starter-jpa. My Entity is annotated with JSR-303 annotations, which are checked in the controller beforethe data gets passed to the CrudRepository for persistence.

为此,我正在使用spring-boot-starter-jpa. 我的实体使用 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 UserUniqueannotation. Its validator looks like this:

我的问题的原因是UserUnique注释。它的验证器看起来像这样:

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 @Validannotation, and once when Hibernate tries to persist the object. However, when Hibernate tries to persist the object, it throws:

似乎正在发生的是,验证正在运行两次。一次通过@Valid注解进入控制器,一次当 Hibernate 尝试持久化对象时。但是,当 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.modewhich you can set to none. However, I cant for the life of me figure out where to set it in a code-based configuration.

似乎有一个名为的属性javax.persistence.validation.mode,您可以将其设置为none。但是,我一生都无法弄清楚在基于代码的配置中将它设置在哪里。

I realise there are questions like JSR-303 dependency injection and Hibernatebut 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. Can anyone help me determine if 1) this is possible and 2) which parts do I need to configure and how?

我想要做的是“后配置”Spring Boot 为我创建的持久层的必需部分,因为如果我定义自己的,那么我就不再利用 Spring Boot 的自动配置。任何人都可以帮助我确定 1) 这是否可行以及 2) 我需要配置哪些部分以及如何配置?

Thanks!

谢谢!

采纳答案by Erin Drummond

As [M. Deinum] mentioned in a comment on my original post, the solution is to set:

作为 [M. Deinum] 在我原帖的评论中提到,解决方法是设置:

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

In the application.propertiesfile.

application.properties文件中。

Additionally, this behaviour is described here(its easy to miss because no example is provided).

此外,这里描述了这种行为(很容易错过,因为没有提供示例)。

回答by Asad Shakeel

@Erin Drummond's Answeris for database entity validation (individual records)

@Erin Drummond 的答案是用于数据库实体验证(单个记录)

But if someone ran into a problem with schema validationbelow property works well.

但是,如果有人遇到以下属性的模式验证问题,则效果很好。

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

回答by John

Actually, in spring boot 2.x. it is:

实际上,在 spring boot 2.x 中。这是:

spring.jpa.hibernate.ddl-auto: none