RelationshipEntity没有持久化

时间:2015-05-15 13:16:14

标签: java spring neo4j spring-data spring-data-neo4j

我遇到关系问题

@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),但如果我稍后导航数据库,那么根本没有跟踪它。 我还尝试使用所涉及节点的集合以不同的方式添加关系; f.e。

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

但完全没有任何改进。

1 个答案:

答案 0 :(得分:1)

您需要更改实体Mashape中关系的方向,(与@StartNode @RelationshipEntity的{​​{1}}对应的实体)。

TagOnObjectEvaluation

请注意,根据@NodeEntity class Mashape { // ... @RelatedToVia(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION, direction = Direction.OUTGOING) private Set<TagOnObjectEvaluation> tagOnObjectEvaluations = new HashSet<TagOnObjectEvaluation>(); } @RelatedToVia注释的specifications,默认情况下的方向为spring-data-neo4j,因此您根本不需要指定在这种情况下的方向。这也应该是正确的:

OUTGOING

希望它有所帮助。