![](/img/trans.png)
[英]How do I use JPA to persist a Map (java.util.Map) object inside an entity and ensure the persistence cascades?
[英]How do I use Spring Data Neo4j to persist a Map (java.util.Map) object inside an NodeEntity?
我需要保存Map<Object,List<Object>>
當我填充包含的節點時節點被保存但Map不是。
這是我為實體使用的代碼
@NodeEntity
public class UserAlias{
@GraphId
private Long id;
@Fetch
private Map<IdentityType,List<Permission>> aliases;
private String name;
}
......
userAliasRepo.save(userAlias)
IdentityType
是Enum, Permission
是另一個未使用@NodeEntity
注釋的類, userAliasRepo
擴展了GraphRepository
<>
那么我如何堅持Map,我是Spring Data Neo4j版本3.3.0.RELEASE
我想要實現的是將以下json與UserAlias NodeEntity相關聯
{
"name": "Bond",
"permissions": {
"Level5Acess": {
"READ": false,
"WRITE": false,
"CREATE": false,
"DEL": true
},
"Level4Acess": {
"READ": false,
"WRITE": false,
"CREATE": false,
"DEL": true
},
"Level1Acess": {
"READ": true,
"WRITE": true,
"CREATE": true,
"DEL": true
},
"Level0Acess": {
"READ": true,
"WRITE": true,
"CREATE": true,
"DEL": true
}
}
}
正如@ frant.hartm指定的那樣
到目前為止,還不支持除Set之外的其他集合類型,目前也沒有Map>。
但是可以使用org.springframework.data.neo4j.fieldaccess.DynamicProperties
代替Map,然后映射到node-attributes。唯一的權衡是它只支持原始dataTypes及其corressponding數組。
@NodeEntity
public class Person {
@GraphId
private Long graphId;
private DynamicProperties personalProperties;
public void setProperty(String key, Object value) {
personalProperties.setProperty(key, value);
}
public Object getProperty(String key) {
return personalProperties.getProperty(key);
}
}
@Test
public void testCreateOutsideTransaction() {
Person p = new Person("James", 35);
p.setProperty("s", "String");
p.setProperty("x", 100);
p.setProperty("pi", 3.1415);
persist(p);
assertEquals(3, IteratorUtil.count(p.getPersonalProperties().getPropertyKeys()));
assertProperties(nodeFor(p));
p.setProperty("s", "String two");
persist(p);
assertEquals("String two", nodeFor(p).getProperty("personalProperties-s"));
}
3.x版本不支持此功能:
到目前為止,不支持除Set之外的其他集合類型,目前還沒有
Map<RelationshipType,Set<NodeBacked>>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.