Bean验证不起作用,春季数据ne4j [英] Bean validation is not working spring data neo4j

查看:5
本文介绍了Bean验证不起作用,春季数据ne4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有嵌入式ne4j的SDN。我必须使用Bean验证,但它不起作用。空值保存在数据库中,没有任何异常。

依赖项

dependencies {
    // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
    // runtime 'mysql:mysql-connector-java:5.1.29'
    // runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
    test "org.grails:grails-datastore-test-support:1.0-grails-2.4"
    compile 'org.springframework.data:spring-data-neo4j:3.2.0.RELEASE'
    compile 'org.hibernate:hibernate-validator:4.3.1.Final'
    compile 'javax.validation:validation-api:1.0.0.GA'      
}

xml config is 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">
<context:component-scan base-package="neo4j"></context:component-scan>
<neo4j:config
storeDirectory="target/db2"
base-package="neo4j"/>

<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

<neo4j:repositories base-package="neo4j" />
</beans>

实体类

@NodeEntity
class Role {
    @GraphId Long graphId

    @NotNull
    String name;

}

控制器

@Transactional
    def saveUser(){

        println "in saveUser"
        Role role = new Role();
        Neo4jTemplate.save(role);

    }

我使用的是Spring-data-new 4j 3.2.0版本

推荐答案

简介

🔴Spring data Neo4j 6.x(您使用的导入Spring-Boot-Starter-Parent:2.4.x的版本)删除了直接从域模型创建约束的可能性

现在,这意味着您必须使用Neo4j的Cypher查询语言并执行您想要执行的操作(例如:CREATE CONSTRAINT notnull_name IF NOT EXISTS ON (role:Role) ASSERT EXISTS (role.name))。

显然,这很难管理,因为您必须在应用程序启动之前执行脚本。而且对于生产环境来说也很困难。

🟢SOLiquigraph已创建:它是为我们执行脚本的工具。

机制如下,我在Github上报告了issue,这很好地解释了一切。


解决方案

💻Pom.xml依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-jdbc-driver</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.liquigraph</groupId>
    <artifactId>liquigraph-spring-boot-starter</artifactId>
    <version>4.0.2</version>
</dependency>

changelog.xml文件>(位于此处-&>💻

<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns="http://www.liquigraph.org/schema/1.0/liquigraph.xsd">
    <changeset id="constraints" author="you">
        <query>CREATE CONSTRAINT notnull_name IF NOT EXISTS ON (role:Role) ASSERT EXISTS (role.name)</query>
    </changeset>
</changelog>

💻应用程序.yml属性

spring:
  neo4j:
    uri: neo4j://localhost:7687                           #neo4j+s if you use an HTTPS Neo4j instance
    authentication:
      username: neo4j
      password: neo4j
  datasource:                                             #Liquigraph configuration used by Liquigraph POM dependency
    url: jdbc:neo4j:neo4j://localhost?encryption=false    #encryption=true if you use an HTTPS Neo4j instance
    driver-class-name: org.neo4j.jdbc.boltrouting.BoltRoutingNeo4jDriver
    username: neo4j
    password: neo4j

✔️##启动时将创建以下约束##

具体日志

2021-01-12 18:33:18.917  INFO 2420 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-01-12 18:33:18.940  INFO 2420 --- [           main] Driver                                   : Routing driver instance 706067443 created for server address localhost:7687
2021-01-12 18:33:21.510  INFO 2420 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-01-12 18:33:22.672  INFO 2420 --- [           main] o.l.core.io.ChangelogGraphWriter         : Executing postcondition of changeset ID constraints by you
2021-01-12 18:33:22.775  INFO 2420 --- [           main] o.l.core.io.ChangelogGraphWriter         : Changeset ID constraints by you was just executed

如您所见,您的into changelog.xml已应用于✔️。

这篇关于Bean验证不起作用,春季数据ne4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆