Interface Record

All Superinterfaces:
MapAccessor, MapAccessorWithDefaultValue

@Immutable public interface Record extends MapAccessorWithDefaultValue
Container for Cypher result values.

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 Details

    • keys

      List<String> keys()
      Retrieve the keys of the underlying map
      Specified by:
      keys in interface MapAccessor
      Returns:
      all field keys in order
    • values

      List<Value> values()
      Retrieve the values of the underlying map
      Specified by:
      values in interface MapAccessor
      Returns:
      all field keys in order
    • index

      int index(String key)
      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 from keys()
    • get

      Value get(int index)
      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

      List<Pair<String,Value>> fields()
      Retrieve all record fields
      Returns:
      all fields in key order
      Throws:
      NoSuchRecordException - if the associated underlying record is not available
    • as

      @Preview(name="Object mapping") default <T> T as(Class<T> targetClass)
      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 of java.lang.Record). The name matching is case-sensitive.

      Additionally, the Property annotation may be used when mapping a property with a different name to java.lang.Record cannonical constructor parameter.

      The constructor selection criteria is the following (top priority first):

      1. Maximum matching properties.
      2. Minimum mismatching properties.
      The constructor search is done in the order defined by the 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 accept null 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) {}
       
       
      Wildcard type value is not supported.
      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 possible
      LossyCoercion - when mapping cannot be achieved without losing precision
      Since:
      5.28.5
      See Also: