Spring Data Neo4J @Indexed(unique = true)不起作用 [英] Spring Data Neo4J @Indexed(unique = true) not working

查看:488
本文介绍了Spring Data Neo4J @Indexed(unique = true)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Neo4J的新手,我有一个简单的问题.

I'm new to Neo4J and I have, probably an easy question.

我的应用程序中有NodeEntitys,用@Indexed(unique = true)注释属性(名称)以实现唯一性,就像我在JPA中使用@Column(unique = true)一样.

There're NodeEntitys in my application, a property (name) is annotated with @Indexed(unique = true) to achieve the uniqueness like I do in JPA with @Column(unique = true).

我的问题是,当我使用图形中已经存在的名称持久化一个实体时,它仍然可以正常工作. 但是我期望这里有某种例外...?! 这是我的基本代码的概述:

My problem is, that when I persist an entity with a name that already exists in my graph, it works fine anyway. But I expected some kind of exception here...?! Here' s an overview over basic my code:

@NodeEntity
public abstract class BaseEntity implements Identifiable
{
    @GraphId
    private Long entityId;
    ...
}

public class Role extends BaseEntity
{
    @Indexed(unique = true)
    private String name;
    ...
}

public interface RoleRepository extends GraphRepository<Role>
{
    Role findByName(String name);
}

@Service
public class RoleServiceImpl extends BaseEntityServiceImpl<Role> implements 
{
    private RoleRepository repository;

    @Override
    @Transactional
    public T save(final T entity) {
    return getRepository().save(entity);
    }
}

这是我的考验:

@Test
public void testNameUniqueIndex() {
    final List<Role> roles = Lists.newLinkedList(service.findAll());
    final String existingName = roles.get(0).getName();
    Role newRole = new Role.Builder(existingName).build();
    newRole = service.save(newRole);
}

这就是我希望出现问题的地方! 如何在不亲自检查的情况下确保属性的唯一性?

That's the point where I expect something to go wrong! How can I ensure the uniqueness of a property, without checking it for myself??

P.S .:我使用的是neo4j 1.8.M07,spring-data-neo4j 2.1.0.BUILD-SNAPSHOT和Spring 3.1.2RELEASE.

P.S.: I'm using neo4j 1.8.M07, spring-data-neo4j 2.1.0.BUILD-SNAPSHOT and Spring 3.1.2.RELEASE.

推荐答案

我走进了同一个陷阱……只要您创建新实体,您就不会看到异常-最后一个 save() -action赢得了胜利.

I walked into the same trap... as long as you create new entities, you will not see the exception - the last save()-action wins the battle.

不幸的是,只有在更新现有实体的情况下,才会引发 DataIntegrityViolationException

Unfortunately, the DataIntegrityViolationException will be raised only in case of update an existing entity!

有关此行为的详细说明,请参见: http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#d5e1035

A detailed description of that behaviour can be found here: http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#d5e1035

这篇关于Spring Data Neo4J @Indexed(unique = true)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆