我是Neo4J的新手,我可能有一个简单的问题。

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

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

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

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

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

请您参考如下方法:

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

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

可以在这里找到有关该行为的详细描述:
http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#d5e1035


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!