You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(object-mapping): Add support for mapping java.lang.Record to value (#1638)
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.
* Assuming movie titles being unique, it is possible to update the created movie in the following way:
426
+
* <pre>
427
+
* {@code
428
+
* var updatedMovie = new Movie("title", "updated tagline", 2024);
429
+
* driver.executableQuery("""
430
+
* MATCH (movie:Movie {title: $movie.title})
431
+
* SET movie += $movie
432
+
* """)
433
+
* .withParameters(Map.of("movie", updatedMovie))
434
+
* .execute();
435
+
* }
436
+
* </pre>
437
+
* The {@link Property} annotation may be used to override the record component name.
438
+
* <pre>
439
+
* {@code
440
+
* public record Movie(String title, String tagline, @Property("releasedYear") long released) {}
441
+
* }
442
+
* </pre>
443
+
* Note that those record components that have {@code null} value will be excluded from the map value.
444
+
* <p>
445
+
* It is also important to understand that sending all properties over network may not always be desirable and will
446
+
* depend on a use-case.
447
+
* <p>
448
+
* In addition, please note that while this mapping allows nested structures, like map of maps, there may be
449
+
* limitations on how those are supported by the database. Please read the Neo4j Cypher Manual for more up-to-date
450
+
* details. For example, at the time of writing, it is not possible to store maps as properties
451
+
* (see the following <a href="https://neo4j.com/docs/cypher-manual/current/values-and-types/property-structural-constructed/#constructed-types">page</a>).
0 commit comments