2 Replies Latest reply on Aug 17, 2010 7:27 AM by 791674

    Persisting objects with EMF results in ID missmatch (DB-ID != object-id)

      Hey guys,

      I got a little problem with database synchronisation using JPA.
      I defined a Entity like the following example:

      +@Table(name = "LOCATION")+
      +public class Location implements Serializable {+
      private static final long serialVersionUID = 1L;
      +@SequenceGenerator(name="location_gen", sequenceName="SEQ_LOCATION_ID")+
      +@Basic(optional = false)+
      +@Column(name = "LOCATION_ID")+
      private long locationId;
      +@Basic(optional = false)+
      +@Column(name = "LOCATION_TYPE")+
      private char LocationType;
      +@Column(name = "LOCATION")+
      private String location;

      There is a simple LocationJpaController like this:

      +public class LocationJpaController{+

      +public LocationJpaController() {+
      emf = Persistence.createEntityManagerFactory("unit12345");
      private EntityManagerFactory emf = null;

      +public EntityManager getEntityManager() {+
      return emf.createEntityManager();

      +public void create(Location location) {+
      EntityManager em = null;
      +try {+
      em = getEntityManager();
      +} finally {+
      +if (em != null) {+

      in my main-method I just create a new Location-Object wihtout an ID and store it via the create-method of LocationJpaController to the database.

      LocationJpaController controller= new LocationJpaController ();
      Location loc = new Location();
      loc.setLocation("My Location 1234");

      After this, the loc object has an ID. The problem is, that the id which is in the database and the refering id in the created location object loc are different.
      For example, loc.getId() returns 10 and in the database is 60 stored.
      But I do not know why they are different. Can you please help me up?

      Edited by: 788671 on 16.08.2010 07:05
        • 1. Re: Persisting objects with EMF results in ID missmatch (DB-ID != object-id)

          Looks likely that your sequence allocation size doesn't match the sequence allocation used in the database. By default, JPA assumes a sequence allocation size of 50, were as most database sequence objects will use a size of 1. Problems will arise when they do not match - in this case it is trying to use 10 (current sequence value of 60 minus the 50 preallocation number).

          add allocationSize=1 or what ever the database sequence allocation size is to the SequenceGenerator annotation.

          Best Regards,
          • 2. Re: Persisting objects with EMF results in ID missmatch (DB-ID != object-id)
            Hi Chris,

            I think you are right with your hint.
            But I'm still not able to fix the problem.

            The defined sequence is:
            Min Value     1
            Max Value     9999999999
            Increment By     1
            Cycle Flag     N
            Order Flag     N
            Cache Size     20
            Last Number     1628

            Like your suggestion, I set the allocationSize=1 in SequenceGenerator annotation.
            After this the id-missmatch is not 50, it is 1. So I tried to set the allocationSize=0 but then of course the application crashes.

            Any ideas?

            Kind regards,

            Okay, I found the problem. Your solution works. I should have set the allocationSize=1. The id missmatch is created by an trigger which is executed before every insert. I disabled the trigger and it works.
            I'm really thankful for your help.

            Edited by: 788671 on 17.08.2010 00:27