-
Notifications
You must be signed in to change notification settings - Fork 155
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236c350
to
369a969
Compare
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.
369a969
to
d3cdec8
Compare
AndyHeap-NeoTech
approved these changes
Apr 23, 2025
MaxAake
approved these changes
Apr 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Very nice addition!
Co-authored-by: Gerrit Meier <[email protected]>
meistermeier
approved these changes
Apr 23, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
:It returns a map value based on record components of a given
java.lang.Record
.Example (similar to the Neo4j Movies Database):
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:Assuming movie titles being unique, it is possible to update the created movie in the following way:
The
Property
annotation may be used to override the record component name.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.