Spring Data Neo4J @Indexed(unique = true)不起作用

Mar*_*amm 6 spring spring-data-neo4j

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

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

我的问题是,当我持有一个名称已经存在于我的图表中的实体时,无论如何它都能正常工作.但是我在这里期待某种例外......?!以下是我的基本代码的概述:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的考验:

@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);
}
Run Code Online (Sandbox Code Playgroud)

这就是我希望出错的地方!我怎样才能确保房产的独特性,而不是自己检查?

PS:我正在使用neo4j 1.8.M07,spring-data-neo4j 2.1.0.BUILD-SNAPSHOT和Spring 3.1.2.RELEASE.

Mic*_*nig 6

我走进了同一个陷阱......只要你创建新实体,你就不会看到异常 - 最后一次保存() -action赢得了战斗.

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

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


lpa*_*zic 5

如果您使用的是SDN 3.2.0+,请使用failOnDuplicate属性:

public class Role extends BaseEntity
{
    @Indexed(unique = true, failOnDuplicate = true)
    private String name;
    ...
}
Run Code Online (Sandbox Code Playgroud)