@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'。你可以试试吗? – troig
你是对的,让它成为答案,以便我可以接受它并为正在发生的事情添加评论 – tigerjack89
为什么在标题中使用***Ŗ***? –