users@glassfish.java.net

JPA Generated Values issue

From: <glassfish_at_javadesktop.org>
Date: Wed, 15 Aug 2007 13:38:40 PDT

Please consider:

[code]
@Entity
@Table(name = "MYTABLE")
public class Mytable implements Serializable {

    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column(name = "DESCRIPTION")
    private String description;

    ...
}
[/code]

And also:

[code]
    public void test() {
        EntityManagerFactory emf = EMFactory.getInstance();
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        Mytable t = new Mytable();
        t.setDescription("This is a test");
        em.merge(t);
        em.getTransaction().commit();
    }
[/code]

This is using the JPA via Java SE, rather than EE (although this happens in Glassfish).

What's happening is that everytime this code is executed, my generated ID goes up by 50.

Now, I understand that by default they use 50 as an increment for the auto generator, but I'm surprised that this happens and gets boosted for every use of the EntityManager, rather than for each use of the EntityManagerFactory. It was my understanding that the EntityManager was lightweight and meant to be used, minimally, once per transaction. So, I'm just kind of surprised to see the AUTO generator consuming so many sequence numbers.

If this were a JEE EJB3 app, I don't see this behavior. It seems to use one 50 block per deploy.

Am I doing something wrong, or is this an expected behaviour?
[Message sent by forum member 'whartung' (whartung)]

http://forums.java.net/jive/thread.jspa?messageID=231024