Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » persisting object with JPA results in id missmatch (db id != java object id)
persisting object with JPA results in id missmatch (db id != java object id) [message #553134] Mon, 16 August 2010 14:32 Go to next message
Markus is currently offline MarkusFriend
Messages: 15
Registered: September 2009
Junior Member
Hey guys,

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

@Entity
@Table(name = "LOCATION")
public class Location implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @SequenceGenerator(name="location_gen",     sequenceName="SEQ_LOCATION_ID")
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="location_gen")
    @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();
            em.getTransaction().begin();
            em.persist(location);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
}

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");
loc.setType('A');
controller.create(loc);


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?
Re: persisting object with JPA results in id missmatch (db id != java object id) [message #553247 is a reply to message #553134] Mon, 16 August 2010 19:28 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Hello,

This seems to be posted here as well:
http://forums.oracle.com/forums/thread.jspa?messageID=450548 9#4505489

Can you verify that the sequence allocation size in the SequenceGenerator annotation matches what is in the database sequence object? By default, it is 50 in JPA.

Best Regards,
Chris
Re: persisting object with JPA results in id missmatch (db id != java object id) [message #553320 is a reply to message #553247] Tue, 17 August 2010 07:29 Go to previous message
Markus is currently offline MarkusFriend
Messages: 15
Registered: September 2009
Junior Member
Hey Chris,

your solution works.
I set the allocationSize=1 in SequenceGenerator annotation.

Thank you for your help.

kind regards,
Markus
Previous Topic:query with JoinTable
Next Topic:PersistenceProvider Service not starting at first time
Goto Forum:
  


Current Time: Thu Mar 28 19:50:30 GMT 2024

Powered by FUDForum. Page generated in 0.01967 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top