Neo4j OGM能否很好地与接口配合使用?

时间:2015-10-02 13:04:49

标签: java neo4j neo4j-ogm

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

这是第一组具有存储库的类 -

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(如果有帮助)。 谢谢, 的Manoj。

1 个答案:

答案 0 :(得分:2)

这应该在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>