我的SkillCluster课程如下
@ExceptionHandler({
javax.validation.ConstraintViolationException.class,
org.hibernate.exception.ConstraintViolationException.class,
org.springframework.dao.DataIntegrityViolationException})
其对应的DAO类
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<>();
}
和我的GenericDAO类为
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;
}
}
我希望通过匹配Node属性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);
答案 0 :(得分:3)
您遇到此错误,因为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;
}
...
}