gpt4 book ai didi

neo4j - Bean 验证不工作 spring 数据 neo4j

转载 作者:行者123 更新时间:2023-12-05 07:57:34 26 4
gpt4 key购买 nike

我正在使用带有嵌入式 neo4j 的 SDN。我必须使用 bean 验证,但它不起作用。null 毫无异常(exception)地保存在数据库中。

依赖是

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;

}

Controller 是

@Transactional
def saveUser(){

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

}

我正在使用 spring-data-neo4j 3.2.0.RELEASE

最佳答案

简介

🔴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) ).

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

🟢所以Liquigraph已经创建:它是一个为我们执行脚本的工具。

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


解决方案

💻 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 文件(位于此处 -> {project_dir}/src/main/resources/db/liquigraph/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>

💻 application.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

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

关于neo4j - Bean 验证不工作 spring 数据 neo4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26653327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com