cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Creating a datetime native type with spring-data-neo4

I'm trying to create a neo4j native datetime type (Temporal (Date/Time) values - Neo4j Cypher Manual) with spring data neo4j. I have a rest controller which takes an object as parameter, this object is then directly saved to neo4j.
I skip the rest controller, I'll give later the implementation of the object.
I'm using

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.0.RELEASE</version>
	</parent>

and all packages with <groupId>org.neo4j</groupId> use version 3.2.2.

I'm using the following pieces of forum and documentation :

Based on this, I wrote an object and a no op converter:
MyObject:

public class MyObject{
    @Convert(NoOpZonedDateTimeConversion.class)
    private ZonedDateTime testDate;
// + getter and setter
}

NoOpZonedDateTimeConversion :

public class NoOpZonedDateTimeConversion implements AttributeConverter<LocalDateTime, LocalDateTime> {
  @Override
  public LocalDateTime toGraphProperty(LocalDateTime value) {
    return value;
  }

  @Override
  public LocalDateTime toEntityAttribute(LocalDateTime value) {
    return value;
  }
}

This registers a string in my database (neo 3.5.5-enterprise).
I tried a few converters with various native java date type, I tried without converter, simple question : is it suppose to work or am I missing something ?

1 ACCEPTED SOLUTION

Sorry it took me a while but just to close this thread :
I run now neo4j 4.1 with docker
I have added in my pom

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-neo4j</artifactId>
      <version>5.3.2.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-ogm-bolt-native-types</artifactId>
      <version>3.2.14</version>
      <scope>runtime</scope>
    </dependency>

In application.yml :

 spring:
    data:
        neo4j:
            use-native-types: "true"

I have a property in a node entity with getters and setters
LocalDateTime date;

And I can now register LocalDateTime directly in my DB which is great necause I have access to all methods and tools of Date and Time in neo4j (better than String imo)

Thanks for the good job.

View solution in original post

3 REPLIES 3

You have to opt-in to enable the native type support. More details in this great blog post (section Native types support) written by @michael.simons.
The key ingredient is to add (assuming you are using bolt protocol) the native type support dependency

<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-ogm-bolt-native-types</artifactId>
    <version>3.2.2</version>
    <scope>runtime</scope>
</dependency>

and enable it either via ogm.properties,

use-native-types=true

the Configuration definition

Configuration configuration = new Configuration.Builder()
        .uri("bolt://neo4j:password@localhost")
        .useNativeTypes()
        .build()

or if you are using Spring Boot for your application you can do this (starting from Spring Boot 2.2) via the application.properties.

spring.data.neo4j.use-native-types=true

Thanks for the help, so far I did not succeed to register a ZonedDateTime native java object in the database as a neo4j DateTime.
I'm using spring boot and I enabled native types in my application.yml. I also added the maven dependancy neo4j-ogm-bolt-native-types.

My NodeEntity has a ZonedateTime private parameter with accessors.
I'm posting a json with this date : "1996-10-15T00:05:32.000Z" and I end up with 1996-10-15T00:05:32[UTC] in the database. I suspect that it might also be a problem with spring boot deserialization of java date types, I'll have to investigate but that will be for another time I'm afraid.

Thanks for the help 🙂

Sorry it took me a while but just to close this thread :
I run now neo4j 4.1 with docker
I have added in my pom

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-neo4j</artifactId>
      <version>5.3.2.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-ogm-bolt-native-types</artifactId>
      <version>3.2.14</version>
      <scope>runtime</scope>
    </dependency>

In application.yml :

 spring:
    data:
        neo4j:
            use-native-types: "true"

I have a property in a node entity with getters and setters
LocalDateTime date;

And I can now register LocalDateTime directly in my DB which is great necause I have access to all methods and tools of Date and Time in neo4j (better than String imo)

Thanks for the good job.