ObjectDB ObjectDB

Primary key generation problem after changing entity package

#1

Hi,

I changed package name for some of my entities in database. I added info in <schema><package... /></schema> of my configuration file - and the changes have been succesfully made in database. Unfortunately, sequences have been probably lost for changed entity classes. After schema change I keep getting errors :

Attempt to reuse an existing primary key value...

My primary keys are annotated as shown below :


@Id

@SequenceGenerator(name = "SEQ_BOARD", allocationSize = 5, initialValue = 1)

@GeneratedValue(generator = "SEQ_BOARD", strategy = GenerationType.SEQUENCE)
edit
delete
#2

Also, when I modify initialValue parameter of existing SequenceGenerator - no changes are made. Changing sequenceName don't help. Only when I change SequenceGenerator name new instances are persisted with correct initialValue setting.

edit
delete
#3

Regarding #1 - I tried the following test:

package com.objectdb.test.forum;

import javax.persistence.*;

public class T719 {

    public static void main(String[] args) {
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:$objectdb/test.odb");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(new A());
        em.getTransaction().commit();
        em.close();
        emf.close();
    }

    @Entity
    public static class A {
        @Id
        @SequenceGenerator(name = "SEQ_BOARD", allocationSize = 5, initialValue = 1)
        @GeneratedValue(generator = "SEQ_BOARD", strategy = GenerationType.SEQUENCE)
        Long id;
   }
}

Then I renamed A to B and added schema update to the configuration:

    <schema>
      <package name="com.objectdb.test.forum">
        <class name="T719$A" new-name="T719$B" />
      </package>
    </schema>

Running the test again didn't cause any error.

Maybe there is a bug when more complex schema changes are made. In addition, notice that any code refactoring that is not synchronized with the configuration may damage the database.

Regarding #2, the initialValue attribute is indeed used only when a new sequence is created.

ObjectDB Support
edit
delete
#4

Thank you for your reply.

 

Regarding #1 :

I ran you test. First, with class A - so that instance of that class is added to database. Then, I renamed this class to B, and put schema information in objectdb.conf file (placed in the same directory as objectdb.jar - lib/). Now when I try to run test again I get error :

Exception in thread "main" [ObjectDB 2.3.7_04] javax.persistence.PersistenceException
Duplicate definition of generator 'SEQ_BOARD in ' in types 'homplex.boostrap.T719$A', 'homplex.boostrap.T719$B' (error 343)
at com.objectdb.jpa.EMImpl.persist(EMImpl.java:378)
at homplex.boostrap.T719.main(T719.java:22)
Caused by: com.objectdb.o.UserException: Duplicate definition of generator 'SEQ_BOARD in ' in types 'homplex.boostrap.T719$A', 'homplex.boostrap.T719$B'
at com.objectdb.o.MSG.d(MSG.java:61)
at com.objectdb.o.TYS.u(TYS.java:549)
at com.objectdb.o.TYS.s(TYS.java:494)
at com.objectdb.o.TYS.o(TYS.java:316)
at com.objectdb.o.TYS.n(TYS.java:290)
at com.objectdb.o.TYM.ad(TYM.java:512)
at com.objectdb.o.TYM.ab(TYM.java:458)
at com.objectdb.o.TYM.an(TYM.java:777)
at com.objectdb.o.TYM.ar(TYM.java:862)
at com.objectdb.o.TYM.av(TYM.java:934)
at com.objectdb.o.OBM.bx(OBM.java:361)
at com.objectdb.o.OBM.bx(OBM.java:249)
at com.objectdb.jpa.EMImpl.persist(EMImpl.java:375)
... 1 more

I add my Eclipse project as attachment.

 

Regarding #2,

When are new sequences created? I tried to put new sequenceName in existing @SequenceGenerator and it didn't work (initialValue not changed). Only when I change @SequenceGenerator name the new sequence is created. And when new sequence is created - is the old one dropped?

 

edit
delete
#5

#1

The error message may indicate that the configuration is not found.

Try putting the objectdb.conf file in the parent directory.

#2 (and please avoid different questions in the same thread, it is messy).

The sequence is generated when it is first found (in that name) and is never deleted.

ObjectDB Support
edit
delete
#6

Ok, yo were right. After putting objectdb.conf in parent directory everything works fine. Unfortunately, in our more complex schema sequences are lost after entities package change.

I will try to create a test case with our schema and send it to you with our database.

edit
delete

Reply

To post on this website please sign in.