Announcement Announcement Module
Collapse
No announcement yet.
[Neo4J] @Indexed(unique = true) not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Neo4J] @Indexed(unique = true) not working

    Hey!

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

    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:

    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);
        }
    }
    And this is my test:

    Code:
        @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??

    THANKS IN ADVANCE FOR ANY IDEAS!!

    P.S.: I'm using neo4j 1.8.M07, spring-data-neo4j 2.1.0.BUILD-SNAPSHOT
    Last edited by markuse1501; Oct 15th, 2012, 10:23 AM.

  • #2
    Neo4j's unique indexes work like that, if the entity is already in the graph you get it back, otherwise it is created for you.

    It is rather a "getOrCreate()" operation. Not so much we can do in the SDN world about it.

    You can compare the object on your side but they should be equal anyway.

    Michael

    Comment


    • #3
      I think I just misunderstood...

      "On saving of the field it will be cross-checked, against the index and fail with a DataIntegrityViolationException if the field was changed to an already existing unique value. Null values are no longer allowed for these properties."

      I expected that exception, in the JPA-way of thinking....

      Comment

      Working...
      X