我的关系有问题

@RelationshipEntity(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION)
public class TagOnObjectEvaluation
{
  @StartNode
  private Mashup taggableObject;

  @EndNode
  private Tag tag;

  // Other fields, getters and setters
}


在涉及的两个实体(MashupTag)中,我都有此字段(方向相反)

@RelatedToVia(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION,
      direction = Direction.INCOMING /*Direction.OUTGOING*/)
  private Set<TagOnObjectEvaluation> tagOnObjectEvaluations =
      new HashSet<TagOnObjectEvaluation>();


然后,我有各种服务类来管理TagMashupTagOnObjectEvaluation。现在正在测试的是后者。
注意:这个名称有点令人困惑,它是以前的编码器的继承人,您可以将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),但是如果我稍后在db上导航,则无法找到它所有。
我还尝试过使用所涉及节点的集合以不同的方式添加关系。 f.e.

tag.getTaggedObjectsEvaluations().add(tagOnObjectEvaluation);
tagDao.update(tag);


但根本没有改善。

最佳答案

您需要更改实体Mashape中的关系的方向(与@StartNode @RelationshipEntityTagOnObjectEvaluation对应的实体)。

@NodeEntity
class Mashape {

   // ...
   @RelatedToVia(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION, direction = Direction.OUTGOING)
   private Set<TagOnObjectEvaluation> tagOnObjectEvaluations = new HashSet<TagOnObjectEvaluation>();

}


只需指出,根据@RelatedToVia spring-data-neo4j注释的specifications,方向默认为OUTGOING,因此在这种情况下,您实际上不需要指定方向。这也应该是正确的:

   @RelatedToVia(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION)
   private Set<TagOnObjectEvaluation> tagOnObjectEvaluations = new HashSet<TagOnObjectEvaluation>();


希望能帮助到你。

07-24 09:15