使用neo4j OGM按节点的属性名称查找节点

阿努拉格·沙玛(Anurag Sharma):

我有我的SkillCluster课程,如下所示

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类

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类为

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
保林·阿穆古(Paulin Amougou):

name遇到此错误,因为该属性不是Long

即使您的name属性Long也是如此,它也不起作用,因为它会被获取错误的节点。

session.load(...)适用于内部节点ID,或标记为@Id或主索引的属性@Index(primary = true)

如果您需要通过节点的属性(而不是主键)来查找节点,则可以使用session.loadAll(...)filter。

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;
 }

 ...

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章