RelationshipEntity 未保留
RelationshipEntity not persisted
我的关系有问题
@RelationshipEntity(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION)
public class TagOnObjectEvaluation
{
@StartNode
private Mashup taggableObject;
@EndNode
private Tag tag;
// Other fields, getters and setters
}
在涉及的两个实体(Mashup
和 Tag
)中,我都有这个字段(方向相反)
@RelatedToVia(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION,
direction = Direction.INCOMING /*Direction.OUTGOING*/)
private Set<TagOnObjectEvaluation> tagOnObjectEvaluations =
new HashSet<TagOnObjectEvaluation>();
然后,我有各种服务 class 来管理 Tag
、Mashup
和 TagOnObjectEvaluation
。现在测试的class是后者。
注意:名称有点混乱,它是以前的编码器遗留下来的,您可以阅读 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
中关系的方向,(实体对应于您@RelationshipEntity
的@StartNode
TagOnObjectEvaluation
).
@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>();
希望对您有所帮助。
我的关系有问题
@RelationshipEntity(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION)
public class TagOnObjectEvaluation
{
@StartNode
private Mashup taggableObject;
@EndNode
private Tag tag;
// Other fields, getters and setters
}
在涉及的两个实体(Mashup
和 Tag
)中,我都有这个字段(方向相反)
@RelatedToVia(type = RelTypes.Tag.TAG_ON_OBJECT_EVALUATION,
direction = Direction.INCOMING /*Direction.OUTGOING*/)
private Set<TagOnObjectEvaluation> tagOnObjectEvaluations =
new HashSet<TagOnObjectEvaluation>();
然后,我有各种服务 class 来管理 Tag
、Mashup
和 TagOnObjectEvaluation
。现在测试的class是后者。
注意:名称有点混乱,它是以前的编码器遗留下来的,您可以阅读 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
中关系的方向,(实体对应于您@RelationshipEntity
的@StartNode
TagOnObjectEvaluation
).
@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>();
希望对您有所帮助。