java Spring数据neo4j Id生成策略
我正在做一个示例项目,其中有一个模型类Movie
,我希望这部电影有一个UUID
作为标识符。
我尝试了以下代码
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.hibernate.annotations.GenericGenerator;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import org.springframework.data.annotation.Id;
import javax.persistence.GeneratedValue;
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@NodeEntity
public class Movie {
@Id @GeneratedValue(generator = "uuid")
@GenericGenerator(name="uuid", strategy="uuid2")
private String id;
private String title;
private int released;
private String tagline;
@Relationship(type = "ACTED_IN", direction = Relationship.INCOMING)
private List<Role> roles = new ArrayList<>();
public Movie() {
}
public Movie(String title, int released, String tagline) {
this.title = title;
this.released = released;
this.tagline = tagline;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public int getReleased() {
return released;
}
public String getTagline() {
return tagline;
}
public Collection<Role> getRoles() {
return roles;
}
public void addRole(Role role) {
this.roles.add(role);
}
}
此代码正在生成错误
org.neo4j.ogm.exception.MappingException: No identity field found for class: movies.spring.data.neo4j.domain.Movie
here是我用来写这段代码的参考
我正在使用neo4j-ogm-core:2.1.3
我不完全理解参考资料中的内容。
但是我需要导入Hibernate
来使用@GenericGenerator
问题:
如何在模型中使用UUID。知道我在用Spring Data
注意:我对Neo4j
和Spring
是新手。我很难提出正确的问题
# 1 楼答案
或者,您可以使用GraphAware Neo4j UUID。根据库REDME文件:
只需下载GraphAware Neo4j Framework和GraphAware Neo4j UUID即可。jar文件到
/plugins
目录,修改neo4j.conf
文件中的few lines并重新启动Neo4j。之后,UUIDs
将被分配给Neo4j图中创建的每个节点/关系这种方法不依赖于Spring