- All Superinterfaces:
MapAccessor
,MapAccessorWithDefaultValue
Streams of records are returned from Cypher query execution, contained
within a Result
.
A record is a form of ordered map and, as such, contained values can be
accessed by either positional index
or textual
key
.
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptiondefault <T> T
Maps values of this record to properties of the given type providing that is it supported.fields()
Retrieve all record fieldsget
(int index) Retrieve the value at the given field indexint
Retrieve the index of the field with the given keykeys()
Retrieve the keys of the underlying mapvalues()
Retrieve the values of the underlying mapMethods inherited from interface org.neo4j.driver.types.MapAccessor
asMap, asMap, containsKey, get, size, values
-
Method Details
-
keys
Retrieve the keys of the underlying map- Specified by:
keys
in interfaceMapAccessor
- Returns:
- all field keys in order
-
values
Retrieve the values of the underlying map- Specified by:
values
in interfaceMapAccessor
- Returns:
- all field keys in order
-
index
Retrieve the index of the field with the given key- Parameters:
key
- the give key- Returns:
- the index of the field as used by
get(int)
- Throws:
NoSuchElementException
- if the given key is not fromkeys()
-
get
Retrieve the value at the given field index- Parameters:
index
- the index of the value- Returns:
- the value or a
NullValue
if the index is out of bounds - Throws:
ClientException
- if record has not been initialized
-
fields
Retrieve all record fields- Returns:
- all fields in key order
- Throws:
NoSuchRecordException
- if the associated underlying record is not available
-
as
Maps values of this record to properties of the given type providing that is it supported.Example (using the Neo4j Movies Database):
// assuming the following Java record public record MovieInfo(String title, String director, List<String> actors) {} // the record values may be mapped to MovieInfo instances var movies = driver.executableQuery("MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)<-[:DIRECTED]-(director:Person) RETURN movie.title as title, director.name AS director, collect(actor.name) AS actors") .execute() .records() .stream() .map(record -> record.as(MovieInfo.class)) .toList();
Note that Object Mapping is an alternative to accessing the user-defined values in a
MapAccessor
. If Object Graph Mapping (OGM) is needed, please use a higher level solution built on top of the driver, like Spring Data Neo4j.The mapping is done by matching user-defined property names to target type constructor parameters. Therefore, the constructor parameters must either have
Property
annotation or have a matching name that is available at runtime (note that the constructor parameter names are typically changed by the compiler unless either the compiler-parameters
option is used or they belong to the cannonical constructor ofjava.lang.Record
). The name matching is case-sensitive.Additionally, the
Property
annotation may be used when mapping a property with a different name tojava.lang.Record
cannonical constructor parameter.The constructor selection criteria is the following (top priority first):
- Maximum matching properties.
- Minimum mismatching properties.
Class.getDeclaredConstructors()
and is finished either when a full match is found with no mismatches or once all constructors have been visited.At least 1 property match must be present for mapping to work.
A
null
value is used for arguments that don't have a matching property. If the argument does not acceptnull
value (this includes primitive types), an alternative constructor that excludes it must be available.The mapping only works for types with directly accessible constructors, not interfaces or abstract types.
Types with generic parameters defined at the class level are not supported. However, constructor arguments with specific types are permitted, see the
actors
parameter in the example above.On the contrary, the following record would not be mapped because the type information is insufficient:
public record MovieInfo(String title, String director, List<T> actors) {}
- Type Parameters:
T
- the target type to map to- Parameters:
targetClass
- the target class to map to- Returns:
- the mapped value
- Throws:
Uncoercible
- when mapping to the target type is not possibleLossyCoercion
- when mapping cannot be achieved without losing precision- Since:
- 5.28.5
- See Also:
-