【问题标题】:RelationshipEntity not persisted关系实体未持久化
【发布时间】: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
}

在涉及的两个实体(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); 

但根本没有任何改进。

【问题讨论】:

  • 也许我错了,但我认为在您的Mashape 实体中,方向应该是OUTOING。可以试试吗?
  • 你是对的,把它作为一个答案,这样我就可以接受它并添加 cmets 来说明发生了什么
  • 为什么在标题中使用Ŗ
  • @Cyber​​neticTwerkGuruOrc wtf,我以为是我的脏屏幕。我什至不知道怎么再做这样的事情:S
  • @tigerjack89 你可能应该编辑它,因为它使这个问题很难用谷歌搜索。

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


【解决方案1】:

你需要在你的实体Mashape中改变关系的方向,(实体对应你的@RelationshipEntityTagOnObjectEvaluation@StartNode)。

@NodeEntity
class Mashape {

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

}

只需根据@RelatedToViaspring-data-neo4j注解的specifications指出,默认方向是OUTGOING,所以这种情况下你真的不需要指定方向。这也应该是正确的:

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

希望对你有帮助。

【讨论】:

  • 是的,看来我的方向颠倒了。根据您的建议,从我所看到的情况来看,当您使用 @RelationshipEntity 类时,相关实体应该引用与 @RelatedToVia(direction = Direction.X) 的关系,其中 X=OUTGOING 表示 @ 中标记为 @StartNode 的实体987654334@ 类,而 X=INCOMING 用于注释 @EndNode 的实体。你知道为什么吗?这似乎与真实情况完全相反。
  • 是的,我认为这取决于您如何表示模型中的关系。对我来说,这是大多数情况下的“逻辑”顺序。很高兴为您提供帮助!
猜你喜欢
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多