Bean validation is not working spring data neo4j

情到浓时终转凉″ 提交于 2021-02-16 18:23:06

问题


I am using SDN with embedded neo4j. i have to use bean validate, but it not working.null is saving in database without any exception.

Dependency is

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>

entity class

@NodeEntity
class Role {
    @GraphId Long graphId

    @NotNull
    String name;

}

controller is

@Transactional
    def saveUser(){

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

    }

I am using spring-data-neo4j 3.2.0.RELEASE


回答1:


Introduction

🔴Spring Data Neo4j 6.x (the one you use importing spring-boot-starter-parent:2.4.x) removed the possibility of creating constraints directly from the domain model.

Now, this means you have to use Neo4j's Cypher query language and execute what you want to do (e.g.: CREATE CONSTRAINT notnull_name IF NOT EXISTS ON (role:Role) ASSERT EXISTS (role.name) ).

Obviously, this is hard to manage, since you'd have to execute a script before your application starts. And it is difficult for production environments.

🟢So Liquigraph has been created: it is a tool that executes, for us, scripts.

The mechanism is the following and I report this issue on Github that explains everything well.


Solution

💻 Pom.xml dependencies:

<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 file (located here -> {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 properties:

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

✔️ ## At the startup, the following constraints will be created ##

Specific Logs:

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

As you can see, your into changelog.xml has been applied ✔️.



来源:https://stackoverflow.com/questions/26653327/bean-validation-is-not-working-spring-data-neo4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!