Conversions
-
Java 类短名称映射到主要标签。
-
转换器使用注册的自定义转换覆盖默认映射。
-
对象字段用于与图形字段转换。
-
使用带有匹配构造函数参数的构造函数,否则使用无参构造函数。
-
同时支持原始类型和包装类型。
-
可以定义自定义转换器以使用自定义类型。
-
@CompositeProperty
可用于将地图存储为复合属性。
Convention-based Mapping
当没有提供任何附加映射元数据时,Neo4j 转换器有几个映射对象的约定。这些约定包括:
The Neo4j Converter has a few conventions for mapping objects when no additional mapping metadata is provided. The conventions are:
-
The short Java class name is mapped to the primary label in the following manner: The class
com.bigbank.SavingsAccount
maps to thesavingsAccount
primary label. -
The converter uses any custom.conversions registered with it to override the default mapping of object properties to node fields and values.
-
The fields of an object are used to convert to and from fields in the graph. Public
JavaBean
properties are not used. -
If you have a single non-zero-argument constructor whose constructor argument names match top-level property names of node, that constructor is used. Otherwise, the zero-argument constructor is used. If there is more than one non-zero-argument constructor, an exception will be thrown.
我们开箱即用地支持广泛的转换。在官方驱动程序手册中找到受支持的密码类型列表: Type mapping。
We support a broad range of conversions out of the box. Find the list of supported cypher types in the official drivers manual: Type mapping.
同样支持包装类型的原始类型。
Primitive types of wrapper types are equally supported.
Domain type | Cypher type | Maps directly to native type |
---|---|---|
|
Boolean |
✔ |
|
List of Boolean |
✔ |
|
Integer |
✔ |
|
List of Integer |
✔ |
|
Float |
✔ |
|
List of Float |
✔ |
|
String |
✔ |
|
List of String |
✔ |
|
ByteArray |
✔ |
|
ByteArray with length 1 |
|
|
String with length 1 |
|
|
List of String with length 1 |
|
|
String formatted as ISO 8601 Date ( |
|
|
String |
|
|
List of String |
|
|
Integer |
|
|
List of Integer |
|
|
String formatted as BCP 47 language tag |
|
|
Integer |
|
|
List of Integer |
|
|
String |
|
|
String |
|
|
Date |
✔ |
|
Time |
✔ |
|
LocalTime |
✔ |
|
DateTime |
✔ |
|
LocalDateTime |
✔ |
|
DateTime |
|
|
DateTime |
|
|
String |
|
|
String |
|
|
Duration |
|
|
Duration |
|
|
Duration |
✔ |
|
Point |
✔ |
|
Point with CRS 4326 |
|
|
Point with CRS 4979 |
|
|
Point with CRS 7203 |
|
|
Point with CRS 9157 |
|
|
Point with CRS 4326 and x/y corresponding to lat/long |
|
Instances of |
String (The name value of the enum) |
|
Instances of |
List of String (The name value of the enum) |
|
|
String |
|
|
String |
|
|
String |
Custom conversions
For attributes of a given type
如果你更喜欢在实体中或作为 @Query
注释方法的参数使用你自己的类型,则可以定义并提供自定义转换器实现。你首先必须实现一个 GenericConverter
并注册你的转换器应处理的类型。对于实体属性类型转换器,你需要注意将你的类型转换为 Neo4j Java Driver Value
和从 Neo4j Java Driver Value
转换你的类型。如果你的转换器仅适用于存储库中的自定义查询方法,则只需提供单向转换到 Value
类型即可。
If you prefer to work with your own types in the entities or as parameters for @Query
annotated methods, you can define and provide a custom converter implementation.
First you have to implement a GenericConverter
and register the types your converter should handle.
For entity property type converters you need to take care of converting your type to and from a Neo4j Java Driver Value
.
If your converter is supposed to work only with custom query methods in the repositories, it is sufficient to provide the one-way conversion to the Value
type.
Unresolved include directive in modules/ROOT/pages/appendix/conversions.adoc - include::example$documentation/repositories/conversion/MyCustomTypeConverter.java[]
要让 SDN 了解你的转换器,必须在 Neo4jConversions
中注册它。为此,你必须使用类型 org.springframework.data.neo4j.core.convert.Neo4jConversions
创建一个 @Bean
。否则,Neo4jConversions
将仅使用内部默认转换器在后台创建。
To make SDN aware of your converter, it has to be registered in the Neo4jConversions
.
To do this, you have to create a @Bean
with the type org.springframework.data.neo4j.core.convert.Neo4jConversions
.
Otherwise, the Neo4jConversions
will get created in the background with the internal default converters only.
Unresolved include directive in modules/ROOT/pages/appendix/conversions.adoc - include::example$documentation/repositories/conversion/MyCustomTypeConverter.java[]
如果你在应用程序中需要多个转换器,则可以在 Neo4jConversions
构造函数中添加尽可能多的转换器。
If you need multiple converters in your application, you can add as many as you need in the Neo4jConversions
constructor.
For specific attributes only
如果你只需要特定属性的转换,我们可以提供 @ConvertWith
。这是一种注释,可用于实体(@Node
)和关系属性(@RelationshipProperties
)的属性。它通过 converter
属性定义 Neo4jPersistentPropertyConverter
并提供可选的 Neo4jPersistentPropertyConverterFactory
来构造前者。借助 Neo4jPersistentPropertyConverter
的实现,可以解决给定类型的特定转换。此外,@ConvertWith
还提供 converterRef
,用于引用应用程序上下文中实现 Neo4jPersistentPropertyConverter
的任何 Spring bean。引用的 bean 优先于构造新的转换器。
If you need conversions only for some specific attributes, we provide @ConvertWith
.
This is an annotation that can be put on attributes of both entities (@Node
) and relationship properties (@RelationshipProperties
)
It defines a Neo4jPersistentPropertyConverter
via the converter
attribute
and an optional Neo4jPersistentPropertyConverterFactory
to construct the former.
With an implementation of Neo4jPersistentPropertyConverter
all specific conversions for a given type can be addressed.
In addition, @ConvertWith
also provides converterRef
for referencing any Spring bean in the application context implementing
Neo4jPersistentPropertyConverter
. The referenced bean will be preferred over constructing a new converter.
我们提供 @DateLong
和 @DateString
作为元注释注释,以便向下兼容不使用原生类型的 Neo4j-OGM 方案。这些是建立在上述概念上的元注释注释。
We provide @DateLong
and @DateString
as meta-annotated annotations for backward compatibility with Neo4j-OGM schemes not using native types.
Those are meta annotated annotations building on the concept above.
Composite properties
使用 @CompositeProperty
,类型为 Map<String, Object>
或 Map<? extends Enum, Object>
的属性可以存储为复合属性。map 中的所有条目都将作为属性添加到包含该属性的节点或关系。使用配置的前缀或使用属性名称作为前缀。虽然我们仅针对地图提供该功能,但你可以使用 Neo4jPersistentPropertyToMapConverter
并将其配置为在 @CompositeProperty
中使用的转换器。Neo4jPersistentPropertyToMapConverter
需要知道如何将给定类型分解为一个 map,以及如何从 map 中重新组合。
With @CompositeProperty
, attributes of type Map<String, Object>
or Map<? extends Enum, Object>
can be stored as composite properties.
All entries inside the map will be added as properties to the node or relationship containing the property.
Either with a configured prefix or prefixed with the name of the property.
While we only offer that feature for maps out of the box, you can Neo4jPersistentPropertyToMapConverter
and configure it
as the converter to use on @CompositeProperty
. A Neo4jPersistentPropertyToMapConverter
needs to know how a given type can
be decomposed to and composed back from a map.