Java 使用neo4j OGM按属性名查找节点
我的技能分类如下Java 使用neo4j OGM按属性名查找节点,java,neo4j,neo4j-ogm,Java,Neo4j,Neo4j Ogm,我的技能分类如下 public class SkillCluster { @Id @GeneratedValue private Long id; private String Name; private String CleanedText; @Relationship(type = "BelongsTo", direction = Relationship.INCOMING) private Set<Skill> contains
public class SkillCluster {
@Id @GeneratedValue
private Long id;
private String Name;
private String CleanedText;
@Relationship(type = "BelongsTo", direction = Relationship.INCOMING)
private Set<Skill> contains = new HashSet<>();
}
我得到以下错误-
java.lang.IllegalArgumentException: Supplied id must be of type Long (native graph id) when supplied class does not have primary id - com.models.SkillCluster
出现此错误是因为
名称
属性不是长
即使您的name属性也是一个长的
,它也不会工作,因为它会被错误的节点获取
session.load(…)
适用于内部节点Id,或使用标记为@Id
或主索引@index(primary=true)
的属性
如果需要通过其属性(而不是主键)查找节点,可以使用带筛选器的session.loadAll(…)
public abstract class GenericDAO<T> {
...
import java.util.Collection;
import java.util.Optional;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.ogm.cypher.Filter;
...
public T find(Long id) {
return session.load(getEntityType(), id, DEPTH_ENTITY);
}
public T find(String name) {
final String propertyName = "name";
Filter filter = new Filter(propertyName, name);
Collection<T> results = session.loadAll(getEntityType(), filter, DEPTH_ENTITY);
if( results.size() > 1)
throw new CustomRuntimesException("Too results found");
Optional<T> entity = results.stream().findFirst();
return entity.isPresent() ? entity.get() : null;
}
...
}
公共抽象类GenericDAO{
...
导入java.util.Collection;
导入java.util.Optional;
导入org.neo4j.graphdb.GraphDatabaseService;
导入org.neo4j.ogm.cypher.Filter;
...
公共找不到(长id){
返回session.load(getEntityType(),id,DEPTH\u ENTITY);
}
公共查找(字符串名称){
最终字符串propertyName=“name”;
过滤器过滤器=新过滤器(propertyName,名称);
收集结果=session.loadAll(getEntityType(),过滤器,深度\实体);
如果(results.size()>1)
抛出新的CustomRuntimeException(“找到太多结果”);
可选实体=results.stream().findFirst();
return entity.isPresent()?entity.get():null;
}
...
}
skillSessionFactory = new SessionFactory(skillConfiguration, "com.skill.models");
skillSession = skillSessionFactory.openSession();
skillClusterDAO = new SkillClusterDAO(skillSession);
SkillCluster clusterNode = skillClusterDAO.find(cluster_name);
java.lang.IllegalArgumentException: Supplied id must be of type Long (native graph id) when supplied class does not have primary id - com.models.SkillCluster
public abstract class GenericDAO<T> {
...
import java.util.Collection;
import java.util.Optional;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.ogm.cypher.Filter;
...
public T find(Long id) {
return session.load(getEntityType(), id, DEPTH_ENTITY);
}
public T find(String name) {
final String propertyName = "name";
Filter filter = new Filter(propertyName, name);
Collection<T> results = session.loadAll(getEntityType(), filter, DEPTH_ENTITY);
if( results.size() > 1)
throw new CustomRuntimesException("Too results found");
Optional<T> entity = results.stream().findFirst();
return entity.isPresent() ? entity.get() : null;
}
...
}