Mocrizza
Mocrizza

Reputation: 15

How to correctly implement class inheritance, annotated as @QueryResult in java neo4j

I'm using neo4j + spring data. To access the data I'm using interfaces, that extends GraphRepository<E>. For example

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

I'm trying to get data, that differs from my domain models. My custom models looks like that

@QueryResult
public class EntryBean {
    @ResultColumn("id")
    private Long id;
    @ResultColumn("name")
    private String name;
    @ResultColumn("content")
    private String content;
    ...
    //getters and setters
}

@QueryResult
public class BoardBean {
    @ResultColumn("id")
    private Long id;
    @ResultColumn("name")
    private String name;
    ...
    //getters and setters
}

Obviously, that it will be better to separate duplicate fields to Base class and inherit from it. So, i'm doing next steps

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

And I don't need BoardBean anymore. But when I'm trying run query

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

I get filled by data just fields that directly declared into EntryBean class (i. e. "content" field). So, How I can correctly implement the @QueryResult class hierarcy?

Upvotes: 1

Views: 339

Answers (1)

fbiville
fbiville

Reputation: 8960

This is a bug which has been present for almost two years (even in 2.3.5.RELEASE!) in the class in charge of converting annotated POJOs.

Indeed, it calls getDeclaredFields on the most concrete type thus skipping possibly inherited annotated fields.

Before the issue is fixed, my piece of advice would be to tolerate this superficial field duplication on your side and not relying on inheritance for now.

Upvotes: 1

Related Questions