【发布时间】:2015-07-27 10:31:51
【问题描述】:
我的关系有问题
@RelationshipEntity(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION)
public class TagOnObjectEvaluation
{
@StartNode
private Mashup taggableObject;
@EndNode
private Tag tag;
// Other fields, getters and setters
}
在涉及的两个实体(Mashup 和 Tag)中,我都有这个字段(方向相反)
@RelatedToVia(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION,
direction = Direction.INCOMING /*Direction.OUTGOING*/)
private Set<TagOnObjectEvaluation> tagOnObjectEvaluations =
new HashSet<TagOnObjectEvaluation>();
然后,我有各种服务类来管理Tag、Mashup 和TagOnObjectEvaluation。现在测试的类是后者。
注意:名称有点混乱,它是以前编码器的遗留物,您可以将 DAO 理解为服务。 GenericNeo4jDAOImpl(同样,读作 GenericServiceNeo4jImpl)也简单地定义了实体管理的标准方法(create()、find()、update()、delete()、fetch())
@Service
public class TagOnObjectEvaluationDAONeo4jImpl extends
GenericNeo4jDAOImpl<TagOnObjectEvaluation> implements
TagOnObjectEvaluationDAO
{
@Autowired
private TagOnObjectEvaluationRepository repository;
public TagOnObjectEvaluationDAONeo4jImpl()
{
super(TagOnObjectEvaluation.class);
}
public TagOnObjectEvaluationDAONeo4jImpl(
Class<? extends TagOnObjectEvaluation> entityClass)
{
super(entityClass);
}
@Override
public TagOnObjectEvaluation create(TagOnObjectEvaluation t)
{
Transaction tx = template.getGraphDatabaseService().beginTx();
TagOnObjectEvaluation savedT = null;
try
{
// This is to enforce the uniqueness of the relationship. I know it can fail in many ways, but this is not a problem ATM
savedT =
template.getRelationshipBetween(
t.getTaggableObject(), t.getTag(),
TagOnObjectEvaluation.class,
RelTypes.Tag.TAG_ON_OBJECT_EVALUATION);
if (savedT == null)
savedT = super.create(t);
tx.success();
}
catch (Exception e)
{
tx.failure();
savedT = null;
}
finally
{
tx.finish();
}
return savedT;
}
}
到目前为止,这似乎很简单。
但是当我尝试持久化 RelationshipEntity 实例时,我遇到了很多问题。
@Test
public void testRelationshipEntityWasPersisted()
{
TagOnObjectEvaluation tagOnObjectEvaluation = new TagOnObjectEvaluation(taggedObject, tag);
tagOnObjectEvaluationDao.create(tagOnObjectEvaluation);
assertNotNull(tagOnObjectEvaluation.getId());
LOGGER.info("TagOnObjectEvaluation id = " + tagOnObjectEvaluation.getId());
tagDao.fetch(tag);
assertEquals(1, tag.getTaggedObjectsEvaluations().size());
}
最后一次测试失败:大小为 0 而不是 1。此外,虽然实体似乎已正确存储(分配了 id),但如果我稍后在数据库中导航,则没有轨道完全没有。
我还尝试使用所涉及节点的集合以不同的方式添加关系; f.e.
tag.getTaggedObjectsEvaluations().add(tagOnObjectEvaluation);
tagDao.update(tag);
但根本没有任何改进。
【问题讨论】:
-
也许我错了,但我认为在您的
Mashape实体中,方向应该是OUTOING。可以试试吗? -
你是对的,把它作为一个答案,这样我就可以接受它并添加 cmets 来说明发生了什么
-
为什么在标题中使用Ŗ?
-
@CyberneticTwerkGuruOrc wtf,我以为是我的脏屏幕。我什至不知道怎么再做这样的事情:S
-
@tigerjack89 你可能应该编辑它,因为它使这个问题很难用谷歌搜索。
标签: java spring neo4j spring-data spring-data-neo4j