Neo4j OGM 是否与接口配合良好?

Does Neo4j OGM work well with interfaces?

我开发了两套 类 - 第一套只是 类,而在第二套中,类 派生自接口。两组 类 相互模仿。它们的存储库也很相似。但是,存储库适用于第一组 类(节点和关系)。尽管对于第二组 类,存储库能够插入记录,但是 findAll 方法失败并且 return 我没有任何记录。

这是第一组 类 与存储库 -

public abstract class Entity {
    @GraphId
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || id == null || getClass() != o.getClass()) return false;

        Entity entity = (Entity) o;
        if (!id.equals(entity.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (id == null) ? -1 : id.hashCode();
    }
}

public abstract class GenericRepository<T> implements IGenericRepository<T> {
    private static final int DEPTH_LIST = 1;

    private static final int DEPTH_ENTITY = 2;

    private Session session;

    public GenericRepository(String url, String username, String password) {
        super();
        session = Neo4jSessionFactory.getInstance().getNeo4jSession(url, username, password);
    }

    public Iterable<T> findAll() {
        return session.loadAll(getEntityType(), DEPTH_LIST);
    }

    public T findOne(Long id) {
        return session.load(getEntityType(), id, DEPTH_ENTITY);
    }

    public void delete(T entity) {
        session.delete(session.load(getEntityType(), ((Entity) entity).getId()));
    }

    public T createOrUpdate(T entity) {
        session.save(entity, DEPTH_ENTITY);
        return findOne(((Entity) entity).getId());
    }

    public abstract Class<T> getEntityType();
}

@RelationshipEntity(type="MY_ROLE")
public class ARole extends Entity {
    @Property
    private String title;

    @StartNode
    private HomoSapiens start;

    @EndNode
    private HomoSapiens end;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public HomoSapiens getStart() {
        return start;
    }

    public void setStart(HomoSapiens start) {
        this.start = start;
    }

    public HomoSapiens getEnd() {
        return end;
    }

    public void setEnd(HomoSapiens end) {
        this.end = end;
    }

    public ARole() {}
    public ARole(String title) {
        this.title = title;
    }
}

@NodeEntity
 public class HomoSapiens extends Entity {
    private String name;

    @Relationship(type = "MY_ROLE", direction = Relationship.INCOMING)
    private ARole aRole;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ARole getaRole() {
        return aRole;
    }

    public void setaRole(ARole aRole) {
        this.aRole = aRole;
    }

    public HomoSapiens() {}
    public HomoSapiens(String name) {
        this.name = name;
    }
}

public class ARoleDao extends GenericRepository<ARole> implements IARoleDao {
    public ARoleDao(String url, String username, String password) {
        super(url, username, password);
    }

    @Override
    public Class<ARole> getEntityType() {
        return ARole.class;
    }
}

这是第二组类-

public interface IARole {
    String getTitle();
    void setTitle(String title);

    IHomoSapiens getStart();
    void setStart(IHomoSapiens start);

    IHomoSapiens getEnd();
    void setEnd(IHomoSapiens end);
}

public interface IHomoSapiens {
    String getName();
    void setName(String name);

    IARole getARole();
    void setARole(IARole aRole);
}

@RelationshipEntity(type="MY_DERIVED_ROLE")
public class DerivedARole extends Entity implements IARole {
    @Property
    private String title;

    @StartNode
    private IHomoSapiens start;

    @EndNode
    private IHomoSapiens end;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public IHomoSapiens getStart() {
        return start;
    }

    public void setStart(IHomoSapiens start) {
        this.start = start;
    }

    public IHomoSapiens getEnd() {
        return end;
    }

    public void setEnd(IHomoSapiens end) {
        this.end = end;
    }

    public DerivedARole() {}
    public DerivedARole(String title) {
        this.title = title;
    }
}

@NodeEntity
public class DerivedHomoSapiens extends Entity implements IHomoSapiens {
    private String name;

    @Relationship(type = "MY_DERIVED_ROLE", direction = Relationship.INCOMING)
    private IARole aRole;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public IARole getARole() {
        return aRole;
    }

    public void setARole(IARole aRole) {
        this.aRole = aRole;
    }

    public DerivedHomoSapiens() {}
    public DerivedHomoSapiens(String name) {
        this.name = name;
    }
}

public class DerivedARoleDao extends GenericRepository<DerivedARole> {
    public DerivedARoleDao(String url, String username, String password) {
        super(url, username, password);
    }

    @Override
    public Class<DerivedARole> getEntityType() {
        return DerivedARole.class;
    }
}

public class DerivedARoleDaoSpecs {
    private DerivedARoleDao derivedARoleDao;

    @Before
    public void setUp() throws Exception {
        derivedARoleDao = new DerivedARoleDao(DomainHelper.NEO_URL, DomainHelper.NEO_USERNAME,
                DomainHelper.NEO_PASSWORD);
    }

    public void should_insert_data() {
        IHomoSapiens start = new DerivedHomoSapiens("d-start");
        IHomoSapiens end = new DerivedHomoSapiens("d-end");
        IARole aRole = new DerivedARole("parent");

        start.setARole(aRole);
        end.setARole(aRole);

        aRole.setStart(start);
        aRole.setEnd(end);

        IARole created = derivedARoleDao.createOrUpdate((DerivedARole)aRole);
        assertThat(created, is(notNullValue()));
    }

    public void should_find_all() {
        Iterable<DerivedARole> derivedARoles = derivedARoleDao.findAll();
        assertThat(derivedARoles, is(notNullValue()));
        assertThat(Iterables.isEmpty(derivedARoles), is(false));
        assertTrue(Iterables.size(derivedARoles) > 0);
        System.out.println(Iterables.size(derivedARoles));
    }

    @Test
    public void should_do_crud() {
//        should_insert_data();
        should_find_all();
//        should_find_by_id();
    }
}

我是不是漏掉了什么?还是 Neo4j OGM 与 类(未实现接口)配合良好?

完整的源代码位于 - https://github.com/mmwaikar/java-neo-ogm-ex(以防万一,它有帮助)。 谢谢, 马诺杰

这应该在 1.1.3 中修复

在发布之前,请尝试使用 1.1.3-SNAPSHOT 的代码示例。

您需要包括

 <repository>
        <id>neo4j-snapshots</id>
        <url>http://m2.neo4j.org/content/repositories/snapshots</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>