Skip to content

feat(object-mapping): Add support for mapping java.lang.Record to value #1638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 23, 2025

Conversation

injectives
Copy link
Contributor

@injectives injectives commented Apr 23, 2025

Please note that this is a feature preview.

This update adds support for mapping java.lang.Record to map value that may be useful when writing data to Neo4j.

The following new method has been added to org.neo4j.driver.Values:

Value value(java.lang.Record record)

It returns a map value based on record components of a given java.lang.Record.

Example (similar to the Neo4j Movies Database):

// assuming the following Java record
public record Movie(String title, String tagline, long released) {}
// a new movie may be created in the following way
var movie = new Movie("title", "tagline", 2025);
var movieValue = Values.value(movie);
driver.executableQuery("CREATE (:Movie $movie)")
         .withParameters(Map.of("movie", movieValue))
         .execute();

Because the driver methods accepting a Map<String, Object> as query parameters automatically map values to value instances, it is possible to avoid mapping movie explicitly:

var movie = new Movie("title", "tagline", 2025);
driver.executableQuery("CREATE (:Movie $movie)")
         .withParameters(Map.of("movie", movie))
         .execute();

Assuming movie titles being unique, it is possible to update the created movie in the following way:

var updatedMovie = new Movie("title", "updated tagline", 2024);
driver.executableQuery("""
                 MATCH (movie:Movie {title: $movie.title})
                 SET movie += $movie
                 """)
         .withParameters(Map.of("movie", updatedMovie))
         .execute();

The Property annotation may be used to override the record component name.

public record Movie(String title, String tagline, @Property("releasedYear") long released) {}

Note that those record components that have null value will be excluded from the map value.

It is also important to understand that sending all properties over network may not always be desirable and will depend on a use-case.

This new mapping as also available via the Value value(Object value) method, but with a note that it is still in preview status, not GA status.

@injectives injectives force-pushed the feature/record-value branch 2 times, most recently from 236c350 to 369a969 Compare April 23, 2025 09:54
Please note that this is a [feature preview](https://github.com/neo4j/neo4j-java-driver/blob/5.0/README.md#preview-features).

This update adds support for mapping `java.lang.Record` to map value that may be useful when writing data to Neo4j.

The following new method has been added to `org.neo4j.driver.Values`:
```java
Value value(java.lang.Record record)
```

It returns a map value based on record components of a given `java.lang.Record`.

Example (similar to the [Neo4j Movies Database](https://github.com/neo4j-graph-examples/movies)):
```java
// assuming the following Java record
public record Movie(String title, String tagline, long released) {}
// a new movie may be created in the following way
var movie = new Movie("title", "tagline", 2025);
var movieValue = Values.value(movie);
driver.executableQuery("CREATE (:Movie $movie)")
         .withParameters(Map.of("movie", movieValue))
         .execute();
```

Because the driver methods accepting a `Map<String, Object>` as query parameters automatically map values to value instances, it is possible to avoid mapping movie explicitly:
```java
var movie = new Movie("title", "tagline", 2025);
driver.executableQuery("CREATE (:Movie $movie)")
         .withParameters(Map.of("movie", movie))
         .execute();
```

Assuming movie titles being unique, it is possible to update the created movie in the following way:
```java
var updatedMovie = new Movie("title", "updated tagline", 2024);
driver.executableQuery("""
                 MATCH (movie:Movie {title: $movie.title})
                 SET movie += $movie
                 """)
         .withParameters(Map.of("movie", updatedMovie))
         .execute();
```

The `Property` annotation may be used to override the record component name.
```java
public record Movie(String title, String tagline, @Property("releasedYear") long released) {}
```

Note that those record components that have `null` value will be excluded from the map value.

It is also important to understand that sending all properties over network may not always be desirable and will depend on a use-case.

This new mapping as also available via the `Value value(Object value)` method, but with a note that it is still in preview status, not GA status.
@injectives injectives force-pushed the feature/record-value branch from 369a969 to d3cdec8 Compare April 23, 2025 09:55
Copy link

@MaxAake MaxAake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Very nice addition!

@injectives injectives merged commit 7698a73 into neo4j:5.0 Apr 23, 2025
19 checks passed
@injectives injectives deleted the feature/record-value branch April 23, 2025 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants