我正在使用嵌入式neo4j的SDN。我必须使用bean验证,但它不工作.null正在保存在数据库中,没有任何异常。
依赖是
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-neo4j 3.2.0.RELEASE
答案 0 :(得分:0)
?Spring Data Neo4j 6.x(您使用导入 spring-boot-starter-parent:2.4.x 的那个)删除了直接从域模型中创建约束的可能性< /strong>。
现在,这意味着您必须使用 Neo4j 的 Cypher 查询语言并执行您想做的事情(例如:CREATE CONSTRAINT notnull_name IF NOT EXISTS ON (role:Role) ASSERT EXISTS (role.name)
)。
显然,这很难管理,因为您必须在应用程序启动之前执行脚本。并且对于生产环境来说很困难。
所以 Liquigraph 已经创建:它是一个工具,为我们执行脚本。
机制如下,我在 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 文件(位于此处 -> {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 已被应用✔️。