如何正确实现类继承,在JavaNeo4j中注释为@QueryResult
我使用的是neo4j+spring数据。为了访问数据,我使用的接口扩展了如何正确实现类继承,在JavaNeo4j中注释为@QueryResult,java,spring,neo4j,Java,Spring,Neo4j,我使用的是neo4j+spring数据。为了访问数据,我使用的接口扩展了graphrespository。比如说 public interface EntryRepository extends GraphRepository<Entry> { @Query("start parent=node({0}), entry=node({1}) " + "match parent-[*1..2{removed:false}]->entry "
graphrespository
。比如说
public interface EntryRepository extends GraphRepository<Entry> {
@Query("start parent=node({0}), entry=node({1}) "
+ "match parent-[*1..2{removed:false}]->entry "
+ "return distinct entry")
Entry findOne(Long parentId, Long entryId);
}
显然,最好将重复字段分离到基类并从基类继承。所以,我正在做下一步
@QueryResult
public class BaseBean {
@ResultColumn("id")
private Long id;
@ResultColumn("name")
private String name;
...
}
@QueryResult
public class EntryBean extends BaseBean{
@ResultColumn("content")
private String content;
...
//getters and setters
}
我不再需要BoardBean了。但当我尝试运行查询时
public interface EntryRepository extends GraphRepository<Entry> {
@Query("start user=node({0}), board=node({1}) "
+ "... "
+ "return id(entry) as id, entry.name as name, entry.content as content")
List<EntryBean> getRelatedEntries(Long userId, Long boardId);
}
public interface EntryRepository扩展了graphrespository{
@查询(“开始用户=节点({0}),线路板=节点({1})”
+ "... "
+“返回id(条目)作为id,entry.name作为名称,entry.content作为内容”)
列出getRelatedEntries(长用户ID、长boardId);
}
我被直接声明到EntryBean
类中的数据字段(即“内容”字段)填充。
那么,我如何才能正确地实现
@QueryResult
类层次结构呢?这是一个已经存在了将近两年的bug(甚至在2.3.5.发行版
!)中,负责转换带注释的POJO
实际上,它对最具体的类型调用getDeclaredFields
,从而跳过可能继承的带注释的字段
在修复之前,我的建议是容忍这种表面上的字段复制,暂时不要依赖继承。您使用的是哪一版本的Spring Data Neo4j?我使用的是3.2.2.Release,其他字段会发生什么情况?这可能是目前实现中检查字段和注释的一个缺点。是的,所有继承的字段都为空。所以,如果我使用较低版本,我可以解决层次结构问题?我不这么认为,这已经存在近2年了。降级到更旧的版本可能会导致代码库中的许多修改(并且可能会暴露在现已修复的bug中)。非常感谢!)这个问题困扰了我几个星期。)@Rolf这是2017年的
。问题解决了吗?我也有同样的问题,我想问题已经解决了。那是我的错误:)
public interface EntryRepository extends GraphRepository<Entry> {
@Query("start user=node({0}), board=node({1}) "
+ "... "
+ "return id(entry) as id, entry.name as name, entry.content as content")
List<EntryBean> getRelatedEntries(Long userId, Long boardId);
}