2015-05-15 35 views
2

我在与关系RelationshipEntity不持久

@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>(); 

然后,我有各种服务级别管理Tag,MashupTagOnObjectEvaluation。现在被测试的类是后者。 注意:该名称有点令人困惑,这是以前编码器的遗留问题,您可以将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

也许我错了,但我认为在你的'Mashape'实体中,方向应该是'OUTOING'。你可以试试吗? – troig

+1

你是对的,让它成为答案,以便我可以接受它并为正在发生的事情添加评论 – tigerjack89

+1

为什么在标题中使用***Ŗ***? –

回答

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>();  

} 

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

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

希望它有帮助。

+0

是的,我似乎倒过来了。从我看到的感谢你的建议,当你使用'@ RelationshipEntity'类时,相关实体应该引用与'@RelatedToVia(direction = Direction.X)'的关系,其中X = OUTGOING实体在@ RelationshipEntity类中标记为@ StartNode,而对于用@ EndNode标注的实体,X = INCOMING。你知道为什么吗?这与真实情况似乎完全相反。 – tigerjack89

+1

是的,我认为它在你如何代表你的模型中的关系。对我而言,这是大多数情况下的“逻辑”顺序。很高兴帮助你! – troig

+1

很快见到你:D – tigerjack89