gpt4 book ai didi

java - RelationshipEntity 未保留

转载 作者:行者123 更新时间:2023-11-30 11:03:42 25 4
gpt4 key购买 nike

我的关系有问题

@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 as Service。此外,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中改变关系的方向,(实体对应于你的@StartNode @RelationshipEntity TagOnObjectEvaluation).

@NodeEntity
class Mashape {

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

}

就根据specifications点那个@RelatedToVia spring-data-neo4j注解的方向默认是OUTGOING,所以你真的不需要指定方向这个案例。这也应该是正确的:

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

希望对您有所帮助。

关于java - RelationshipEntity 未保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30260530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com