Conversions
-
Java 类短名称映射到主要标签。
-
转换器使用注册的自定义转换覆盖默认映射。
-
对象字段用于与图形字段转换。
-
使用带有匹配构造函数参数的构造函数,否则使用无参构造函数。
-
同时支持原始类型和包装类型。
-
可以定义自定义转换器以使用自定义类型。
-
@CompositeProperty
可用于将地图存储为复合属性。
Convention-based Mapping
当没有提供任何附加映射元数据时,Neo4j 转换器有几个映射对象的约定。这些约定包括:
-
简短 Java 类名按照以下方式映射到主要标签:类
com.bigbank.SavingsAccount
映射到savingsAccount
主要标签。 -
转换器使用向它注册的任何 Spring Converter 来覆盖对象属性到节点字段和值的默认映射。
-
对象的字段用于在图中的字段之间进行转换。不使用公共
JavaBean
属性。 -
如果您有一个单一非零参数构造函数,并且其构造函数参数名称与节点的顶级属性名称匹配,则使用该构造函数。否则,将使用零参数构造函数。如果有多个非零参数构造函数,将抛出异常。
我们开箱即用地支持广泛的转换。在官方驱动程序手册中找到受支持的密码类型列表: Type mapping。
同样支持包装类型的原始类型。
Domain type | Cypher 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 |
|
|
长度为 1 的字符串列表 |
|
|
字符串,格式为 ISO 8601 日期 ( |
|
|
String |
|
|
List of String |
|
|
Integer |
|
|
List of Integer |
|
|
字符串,格式为 BCP 47 语言标签 |
|
|
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 |
|
|
带有 CRS 4326 的点,其中的 x/y 对应于纬度/经度 |
|
Instances of |
字符串(枚举的名称值) |
|
Instances of |
字符串列表(枚举的名称值) |
|
|
String |
|
|
String |
|
|
String |
Custom conversions
For attributes of a given type
如果你更喜欢在实体中或作为 @Query
注释方法的参数使用你自己的类型,则可以定义并提供自定义转换器实现。你首先必须实现一个 GenericConverter
并注册你的转换器应处理的类型。对于实体属性类型转换器,你需要注意将你的类型转换为 Neo4j Java Driver Value
和从 Neo4j Java Driver Value
转换你的类型。如果你的转换器仅适用于存储库中的自定义查询方法,则只需提供单向转换到 Value
类型即可。
link:example$documentation/repositories/conversion/MyCustomTypeConverter.java[role=include]
要让 SDN 了解你的转换器,必须在 Neo4jConversions
中注册它。为此,你必须使用类型 org.springframework.data.neo4j.core.convert.Neo4jConversions
创建一个 @Bean
。否则,Neo4jConversions
将仅使用内部默认转换器在后台创建。
link:example$documentation/repositories/conversion/MyCustomTypeConverter.java[role=include]
如果你在应用程序中需要多个转换器,则可以在 Neo4jConversions
构造函数中添加尽可能多的转换器。
For specific attributes only
如果你只需要特定属性的转换,我们可以提供 @ConvertWith
。这是一种注释,可用于实体(@Node
)和关系属性(@RelationshipProperties
)的属性。它通过 converter
属性定义 Neo4jPersistentPropertyConverter
并提供可选的 Neo4jPersistentPropertyConverterFactory
来构造前者。借助 Neo4jPersistentPropertyConverter
的实现,可以解决给定类型的特定转换。此外,@ConvertWith
还提供 converterRef
,用于引用应用程序上下文中实现 Neo4jPersistentPropertyConverter
的任何 Spring bean。引用的 bean 优先于构造新的转换器。
我们提供 @DateLong
和 @DateString
作为元注释注释,以便向下兼容不使用原生类型的 Neo4j-OGM 方案。这些是建立在上述概念上的元注释注释。
Composite properties
使用 @CompositeProperty
,类型为 Map<String, Object>
或 Map<? extends Enum, Object>
的属性可以存储为复合属性。map 中的所有条目都将作为属性添加到包含该属性的节点或关系。使用配置的前缀或使用属性名称作为前缀。虽然我们仅针对地图提供该功能,但你可以使用 Neo4jPersistentPropertyToMapConverter
并将其配置为在 @CompositeProperty
中使用的转换器。Neo4jPersistentPropertyToMapConverter
需要知道如何将给定类型分解为一个 map,以及如何从 map 中重新组合。