openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Dick <michael.d.d...@gmail.com>
Subject Re: Issues with GeneratedValue and SequenceGenerator
Date Tue, 05 Apr 2011 23:35:20 GMT
When you use GenerationType.IDENTITY or GenerationType.SEQUENCE the ID
values aren't known until the row is inserted. OpenJPA does not
automatically refresh this information, but you can obtain it if you use the
EntityManager.refresh() method.

Regarding intialValues, did OpenJPA create the sequence in the database? If
the sequence already exists OpenJPA will not update the definition (I think
even SynchronizeMappings will leave an existing Sequence alone). It might
help to manually drop and let OpenJPA recreate the sequence.

-mike

On Tue, Apr 5, 2011 at 5:36 PM, realdepp <thepsycho@freenet.de> wrote:

> Hi!
>
> I'm working with OpenJPA 2.1.0 and have (simplified) the following
> superclass. All "real" entities derive from that class:
>
> @MappedSuperclass
> public abstract class MyBase {
>    private long id;
>
>    @Id
>    @GeneratedValue(strategy = GenerationType.IDENTITY)
>    public final long getId() {
>        return id;
>    }
>
>    public final void setId(long id) {
>        this.id = id;
>    }
> }
>
> I'm persisting the classes with the following code:
>
>    EntityTransaction et = em.getTransaction();
>    et.begin();
>    em.persist(myEntity);
>    et.commit();
>    // Huh!?
>    System.out.println(myEntity.getId() + " == 0");
>
> The System.out always sais "0 == 0", however in the database there is a
> correct generated id.
>
> Am I missing something?
>
>
> Another thing:
>
> The same scenario as above, but a different strategy for @GeneratedValue:
>    @Id
>    @SequenceGenerator(name = "My_Seq", initialValue = 100000,
> allocationSize = 1)
>    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
> "My_Seq")
>
> The same error as above, but additionally, the "initialValue" is ignored,
> the ID in the db starts with "1".
> Did I trigger a bug or did I make a mistake?
>
>
> Thanks a lot.
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6244055.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message