spring-boot hibernate关闭验证

时间:2016-12-30 23:13:34

标签: spring hibernate validation spring-boot

我遇到了与How to disable Hibernate validation in a Spring Boot project相同的问题并做了同样的事情。在application.yml中我有:

jsf:
  PROJECT_STAGE: Development
  primefaces:
    theme: overcast
logging:
  level:
    root: debug


spring:
  datasource:
    initialize: false
    password: heslo
    username: MoneyTracker
    url: jdbc:postgresql://localhost:5432/MT-test
  jpa:
    properties:
      javax:
        persistence:
          validation:
            mode: none
    hibernate:
      ddl-auto: update

因此验证模式应该设置为none,但它看起来不是。在日志中我可以看到:

2016-12-30 23:39:44.166  INFO 63192 --- [on(2)-127.0.0.1] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2016-12-30 23:39:44.215 DEBUG 63192 --- [on(2)-127.0.0.1] o.hibernate.jpa.internal.util.LogHelper  : PersistenceUnitInfo [
        name: default
        persistence provider classname: null
        classloader: ParallelWebappClassLoader
  context: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@6d21714c

        excludeUnlistedClasses: true
        JTA datasource: null
        Non JTA datasource: org.apache.tomcat.jdbc.pool.DataSource@6798b962{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=org.postgresql.Driver; maxAct
ive=100; maxIdle=100; minIdle=10; initialSize=10; maxWait=30000; testOnBorrow=true; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=
false; password=********; url=jdbc:postgresql://localhost:5432/MT-test; username=MoneyTracker; validationQuery=SELECT 1; validationQueryTimeout=-1; validatorClassName=null; validationInterval=3000; accessToUnderlyingConnectionAllowed=t
rue; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=null; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=
false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; 
ignoreExceptionOnPreLoad=false; }
        Transaction type: RESOURCE_LOCAL
        PU root URL: file:/Users/romanrakus/Tomcats/apache-tomcat-8.5.4/webapps/ROOT/WEB-INF/lib/database-1.0-SNAPSHOT.jar
        Shared Cache Mode: UNSPECIFIED
        Validation Mode: AUTO
        Jar files URLs []
        Managed classes names [
                cz.romanrakus.moneytracker.db.entities.Person
                cz.romanrakus.moneytracker.db.entities.PersonRole
                cz.romanrakus.moneytracker.db.entities.SpendingType
                cz.romanrakus.moneytracker.db.entities.PredefinedCsvImport
                cz.romanrakus.moneytracker.db.entities.Spending]
        Mapping files names []
        Properties []

所有设置都已正确加载,但验证模式未正确加载。我做错了吗?

还有关于加载验证模式属性的日志行:

2016-12-30 23:50:01.534 DEBUG 63192 --- [on(7)-127.0.0.1] org.springframework.jndi.JndiTemplate    : Looking up JNDI object with name [java:comp/env/spring.jpa.properties.javax.persistence.validation.mode]
2016-12-30 23:50:01.534 DEBUG 63192 --- [on(7)-127.0.0.1] o.s.jndi.JndiLocatorDelegate             : Converted JNDI name [java:comp/env/spring.jpa.properties.javax.persistence.validation.mode] not found - trying original name [spring.jpa.properties.javax.persistence.validation.mode]. javax.naming.NameNotFoundException: Name [spring.jpa.properties.javax.persistence.validation.mode] is not bound in this Context. Unable to find [spring.jpa.properties.javax.persistence.validation.mode].
2016-12-30 23:50:01.534 DEBUG 63192 --- [on(7)-127.0.0.1] org.springframework.jndi.JndiTemplate    : Looking up JNDI object with name [spring.jpa.properties.javax.persistence.validation.mode]
2016-12-30 23:50:01.534 DEBUG 63192 --- [on(7)-127.0.0.1] o.s.jndi.JndiPropertySource              : JNDI lookup for name [spring.jpa.properties.javax.persistence.validation.mode] threw NamingException with message: Name [spring.jpa.properties.javax.persistence.validation.mode] is not bound in this Context. Unable to find [spring.jpa.properties.javax.persistence.validation.mode].. Returning null.
2016-12-30 23:50:01.534 DEBUG 63192 --- [on(7)-127.0.0.1] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'spring.jpa.properties.javax.persistence.validation.mode' in [applicationConfigurationProperties] with type [String]

为什么验证模式仍然设置为AUTO?还有什么我可以做的是从spring开始注入bean的验证器类?

更新

在调试过程中,我发现验证模式已正确解析并设置(即使日志中还有其他内容)。但是应用程序仍在使用hibernate验证器而不是spring验证器。我还想到了属性javax.persistence.validation.factory来包含对名为validator的Bean的引用。但仍然没有运气:(

我正在使用WebSecurityConfigurerAdapter,我不知道它是否重要。

1 个答案:

答案 0 :(得分:0)

原来我的问题非常不同。我不得不在JSF中添加验证器(spring constraint validator)。适合我的解决方案是stackoverflow.com/a/21279420/5788897