2 回答

TA貢獻1810條經(jīng)驗 獲得超5個贊
看看AttributeConverter注釋,但是如果您需要關(guān)系上的值集合,請考慮重構(gòu)您的模型,使其成為具有相關(guān)內(nèi)容的節(jié)點。
例子:
這是一個示例屬性轉(zhuǎn)換器(在 Kotlin 中),它在 Neo4j 中將字符串數(shù)組屬性轉(zhuǎn)換為/從字符串數(shù)組屬性轉(zhuǎn)換為 Java 類型。
class RoleArrayAttributeConverter : AttributeConverter<Array<Role>, Array<String>>
{
override fun toEntityAttribute(value: Array<String>): Array<Role>
{
return value.map { Role.valueOf(it) }.toTypedArray()
}
override fun toGraphProperty(value: Array<Role>): Array<String>
{
return value.map { it.toString() }.toTypedArray()
}
}

TA貢獻1827條經(jīng)驗 獲得超8個贊
根據(jù)@Jasper Blues 的建議,我用 Java 創(chuàng)建了自己的轉(zhuǎn)換器?;卮鹞易约旱膯栴},因為我無法在評論中添加這個。
public class ChildConverter implements AttributeConverter<Set<Child>, String> {
ObjectMapper mapper = new ObjectMapper();
@Override
public String toGraphProperty(Set<Child> data) {
String value = "";
try {
value = mapper.writeValueAsString(data);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return value;
}
@Override
public Set<Child> toEntityAttribute(String data) {
Set<Child> mapValue = new HashSet<Child>();
TypeReference<Set<Child>> typeRef = new TypeReference<Set<Child>>() {
};
try {
mapValue = mapper.readValue(data, typeRef);
} catch (IOException e) {
e.printStackTrace();
}
return mapValue;
}
}
確保在父類中添加@Convert 注解。
@Convert(converter = ChildConverter.class)
Set<Child> = new HashSet<>();
添加回答
舉報