gpt4 book ai didi

java - Neo4j OGM 与界面配合良好吗?

转载 作者:行者123 更新时间:2023-12-02 04:19:54 27 4
gpt4 key购买 nike

我开发了两组类 - 第一个只是类,而在第二组中,类派生自接口(interface)。两组类都互相模仿。它们的存储库也相似。但是,存储库对于第一组类(节点和关系)效果很好。对于第二组类,存储库能够插入记录,但 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 是否可以与类(不实现接口(interface))很好地配合?

整个源代码位于 - 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>

关于java - Neo4j OGM 与界面配合良好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32907836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com