如何将neo4j Id更改为UUID并使finder方法工作?
Neo4j需要一个长类型的id字段才能工作。这在Spring数据neo4j中运行良好。我希望有另一个类型为UUID的字段,并让T findOne(T id)使用我的UUID而不是neo生成的id 因为我使用的是SpringDataREST,所以我不想在URL中公开neo的id {neoId} 到 {uuid} 如果可能的话,有什么想法吗 更新如何将neo4j Id更改为UUID并使finder方法工作?,neo4j,spring-data-neo4j,spring-data-rest,Neo4j,Spring Data Neo4j,Spring Data Rest,Neo4j需要一个长类型的id字段才能工作。这在Spring数据neo4j中运行良好。我希望有另一个类型为UUID的字段,并让T findOne(T id)使用我的UUID而不是neo生成的id 因为我使用的是SpringDataREST,所以我不想在URL中公开neo的id {neoId} 到 {uuid} 如果可能的话,有什么想法吗 更新 { name: "Root", resourceId: "00671e1a-4053-4a68-9c59-f870915e3257",
{
name: "Root",
resourceId: "00671e1a-4053-4a68-9c59-f870915e3257",
_links: {
self: {
href: "http://localhost:8080/resource/9750"
},
parents: {
href: "http://localhost:8080/resource/9750/parents"
},
children: {
href: "http://localhost:8080/resource/9750/children"
}
}
}
您可以向实体添加一个字符串属性,称之为uuid,只需在E的存储库中声明一个E FindByUID(字符串uuid),Spring数据将自动为其生成代码。
例如:
@NodeEntity
public class Entity {
...
@Indexed
private String uuid;
...
public String getUuid() {
return uuid;
}
void setUuid(String uuid) {
this.uuid = uuid;
}
...
}
public interface EntityRepository extends GraphRepository<Entity> {
...
Entity findByUuid(String uuid);
...
}
@NodeEntity
公共类实体{
...
@索引
私有字符串uuid;
...
公共字符串getUuid(){
返回uuid;
}
void setUuid(字符串uuid){
this.uuid=uuid;
}
...
}
公共接口EntityRepository扩展了GraphRepository{
...
实体findbyuid(字符串uuid);
...
}
当涉及到存储库中的finder方法时,您可以在自己的界面中覆盖crudepository
中提供的方法,或者提供另一种方法,即T findByUuid(长uuid)
根据您建模类的方式,您可以依赖于方法名称中的派生查询,也可以使用查询进行注释,例如:
@Query(value = "MATCH (n:YourNodeType{uuid:{0}) RETURN n")
如果要使用特定的UUID类,则需要告诉Neo如何持久化UUID值。如果您准备将其存储为字符串(看起来很合理),那么我认为不需要对字段进行注释,其他任何操作都需要GraphProperty
注释:
@GraphProperty(propertyType = Long.class)
同样,根据UUID的类别,您可能需要向Spring注册一个转换类,Spring实现org.springframework.core.convert.converter.converter
接口,并在域类类型(UUID)和存储类型(字符串)之间进行转换
或者,只需将UUID转换为字符串并自己存储,不必担心所有的转换
无论您做什么,请确保您的新
uuid
已编制索引,并且可能是唯一的。谢谢John,我找到了我想要的查找方法。但是,指向self、parent和children的链接仍然具有节点id,请参见更新的问题。这是显而易见的,但有没有办法将其更改为UUID呢