使用 neo4j OGM 按 属性 名称查找节点
Find node by its property name using neo4j OGM
我的 SkillCluster class 如下
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<>();
}
对应的DAO class
import org.neo4j.ogm.session.Session;
import com.models.GenericDAO;
public class SkillClusterDAO extends GenericDAO<SkillCluster>{
public SkillClusterDAO(Session session) {
super(session);
}
protected Class<SkillCluster> getEntityType() {
return SkillCluster.class;
}
}
和我的 GenericDAO class 作为
public abstract class GenericDAO<T> {
private static final int DEPTH_LIST = 0;
private static final int DEPTH_ENTITY = 1;
private Session session;
public long filterCount(Iterable<Filter> filters){
return session.count(getEntityType(), filters);
}
public T find(Long id) {
return session.load(getEntityType(), id, DEPTH_ENTITY);
}
public T find(String name) {
return session.load(getEntityType(), name, DEPTH_ENTITY);
}
public void delete(Long id) {
session.delete(session.load(getEntityType(), id));
}
public void createOrUpdate(T entity) {
session.save(entity, DEPTH_ENTITY);
//return find(entity.id);
}
protected abstract Class<T> getEntityType();
public GenericDAO(Session session) {
this.session = session;
}
}
我想通过匹配节点 属性 Name
来获取集群节点
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
您出现此错误是因为 name
属性 不是 Long
。
即使你的名字 属性 也是 Long
,它也不会工作,因为它会被提取到错误的节点。
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;
}
...
}
我的 SkillCluster class 如下
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<>();
}
对应的DAO class
import org.neo4j.ogm.session.Session;
import com.models.GenericDAO;
public class SkillClusterDAO extends GenericDAO<SkillCluster>{
public SkillClusterDAO(Session session) {
super(session);
}
protected Class<SkillCluster> getEntityType() {
return SkillCluster.class;
}
}
和我的 GenericDAO class 作为
public abstract class GenericDAO<T> {
private static final int DEPTH_LIST = 0;
private static final int DEPTH_ENTITY = 1;
private Session session;
public long filterCount(Iterable<Filter> filters){
return session.count(getEntityType(), filters);
}
public T find(Long id) {
return session.load(getEntityType(), id, DEPTH_ENTITY);
}
public T find(String name) {
return session.load(getEntityType(), name, DEPTH_ENTITY);
}
public void delete(Long id) {
session.delete(session.load(getEntityType(), id));
}
public void createOrUpdate(T entity) {
session.save(entity, DEPTH_ENTITY);
//return find(entity.id);
}
protected abstract Class<T> getEntityType();
public GenericDAO(Session session) {
this.session = session;
}
}
我想通过匹配节点 属性 Name
来获取集群节点
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
您出现此错误是因为 name
属性 不是 Long
。
即使你的名字 属性 也是 Long
,它也不会工作,因为它会被提取到错误的节点。
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;
}
...
}