Announcement Announcement Module
No announcement yet.
[neo4j] Auto Increment ID for node Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • [neo4j] Auto Increment ID for node

    Like other databases, is there any auto increment ID support from neo4j / SDN for nodes ?

    For example in the following example I want to the variable id to be unique and automatically incremented everytime a node gets checked in.
    I was trying to maintain one incrementing ID, by counting the nodes, saving it to database etc. But it would be better if there is an existing support
    for autoimcrementing unique keys

    public class A {
    @Indexed(unique=true) long id; // I want this to be incremented automatically and be unique
    public A() {

  • #2
    SDN added before-save listeners in 2.1.0.RC4, so you can use those and a (static) AtomicLong in your listener (or entity-class) as source for your ID's which is e.g. stored on the reference-node and read-back on startup?



    • #3
      Thanks for your reply Michael.
      I am using a similar approach, but implementing the TransactionEventHandler interface and doing ID generation thing in beforeCommit(TransactionData data). This will work, right ?

      One other observation is, if we try to insert an object with duplicate value in unique field, the execution point comes into beforeCommit(TransactionData data) and do ID generation, but finally ends up creating no node in the database. This is basically creating gaps in the incrementing IDs we are generating.

      But if you do something like, creating two objects of same class with different values in their unique field and then try to change the unique field value of one object to that of the other object, it throws DataIntegrityException and never reaches the beforeCommit(TransactionData data). So the ID does not get generated and no gaps or holes get created in the IDs we are generating.

      Any solution to the first one ? Will before-save solve this problem ? (i.e., I mean if I implement the ID generation in it, it will not increment the ID, when inserting the objects with duplicate values in unique field ?)

      Thanks in advance


      • #4
        It is also deleted (in the implementation of UniqueNodeFactory) so it should show up in the deleted part of the transaction data. If you can check that and then don't create a new unique-id it should work.