You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by realdepp <th...@freenet.de> on 2011/04/06 00:36:08 UTC

Issues with GeneratedValue and SequenceGenerator

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.

Re: Issues with GeneratedValue and SequenceGenerator

Posted by realdepp <th...@freenet.de>.
I forgot to add it here - I opened a bug report:
https://issues.apache.org/jira/browse/OPENJPA-1977


--
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6265927.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with GeneratedValue and SequenceGenerator

Posted by Jeremy Bauer <te...@gmail.com>.
I agree.  The OpenJPA documentation is confusing/contradictory.

1.2.  Final

Entity classes may not be final. No method of an entity class can be final.

*Note*

OpenJPA supports final classes and final methods.

I wonder if the distinction is "Entity" vs. a plain old class?  Either
way, I think the note should be removed from the documentation.

-Jeremy

On Mon, Apr 11, 2011 at 2:35 PM, Rick Curtis <cu...@gmail.com> wrote:
> 1.2.  Final
>
> Entity classes may not be final. No method of an entity class can be final.
> Note
>
> OpenJPA supports final classes and final methods.
> Confusing? Perhaps. It seems pretty clear that we don't support final
> Entities or final Entity methods. I'm not sure what the "Note" is referring
> to though.
>
> If you want, open a JIRA and I can remove that Note. It's more confusing
> than anything.
>
> Thanks,
> Rick
>
> On Mon, Apr 11, 2011 at 2:23 PM, realdepp <th...@freenet.de> wrote:
>
>> I'm not sure about that - the final modifier seems to be the cause for the
>> problem.
>>
>> The JPA spec sais:
>> "Entity classes may not be final. No method of an entity class can be
>> final."
>>
>> BUT the OpenJPA docs say:
>> "OpenJPA supports final classes and final methods."
>>
>> http://openjpa.apache.org/builds/2.1.0/apache-openjpa-2.1.0/docs/manual/jpa_overview_pc.html#jpa_overview_pc_final
>>
>> So this is either an implementation bug or a documentation bug
>

Re: Issues with GeneratedValue and SequenceGenerator

Posted by Rick Curtis <cu...@gmail.com>.
1.2.  Final

Entity classes may not be final. No method of an entity class can be final.
Note

OpenJPA supports final classes and final methods.
Confusing? Perhaps. It seems pretty clear that we don't support final
Entities or final Entity methods. I'm not sure what the "Note" is referring
to though.

If you want, open a JIRA and I can remove that Note. It's more confusing
than anything.

Thanks,
Rick

On Mon, Apr 11, 2011 at 2:23 PM, realdepp <th...@freenet.de> wrote:

> I'm not sure about that - the final modifier seems to be the cause for the
> problem.
>
> The JPA spec sais:
> "Entity classes may not be final. No method of an entity class can be
> final."
>
> BUT the OpenJPA docs say:
> "OpenJPA supports final classes and final methods."
>
> http://openjpa.apache.org/builds/2.1.0/apache-openjpa-2.1.0/docs/manual/jpa_overview_pc.html#jpa_overview_pc_final
>
> So this is either an implementation bug or a documentation bug

Re: Issues with GeneratedValue and SequenceGenerator

Posted by realdepp <th...@freenet.de>.
I'm not sure about that - the final modifier seems to be the cause for the
problem.

The JPA spec sais:
"Entity classes may not be final. No method of an entity class can be
final."

BUT the OpenJPA docs say:
"OpenJPA supports final classes and final methods."
http://openjpa.apache.org/builds/2.1.0/apache-openjpa-2.1.0/docs/manual/jpa_overview_pc.html#jpa_overview_pc_final

So this is either an implementation bug or a documentation bug.

--
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6262607.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with GeneratedValue and SequenceGenerator

Posted by realdepp <th...@freenet.de>.
I think I got it:
When I mark the getter and the setter for the id as "final", the error
occurs.
Without final it works fine.


--
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6249036.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with GeneratedValue and SequenceGenerator

Posted by Michael Dick <mi...@gmail.com>.
Well, I was able to reproduce if I do the following

1. Create the database tables.
2. Run the PCEnhancer with BaseEntity and MyEntity in the persistence.xml
3. Remove BaseEntity from persistence.xml
4. Persist a new instance of MyEntity.

I've only reproduced with IDENTITY, but it should be similar with SEQUENCE.

Otherwise I've had no luck. Based on your results it sounds like the
database was not created properly, but that's just a guess.

-mike

On Wed, Apr 6, 2011 at 2:46 PM, realdepp <th...@freenet.de> wrote:

> Yes, MyBase is in persistence.xml.
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6247366.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: Issues with GeneratedValue and SequenceGenerator

Posted by realdepp <th...@freenet.de>.
Yes, MyBase is in persistence.xml.

--
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6247366.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with GeneratedValue and SequenceGenerator

Posted by realdepp <th...@freenet.de>.
I don't know whats happening here - I tested with MySQL 5.1.31 and it worked.
Then I switched back to MySQL 4.1.22 and it worked, too.

So I'm not able to reproduce the error for now - but I have no idea why.

--
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6247365.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with GeneratedValue and SequenceGenerator

Posted by Michael Dick <mi...@gmail.com>.
Is the MyBase class listed in persistence.xml?

-mike

On Wed, Apr 6, 2011 at 1:54 PM, realdepp <th...@freenet.de> wrote:

> MySQL version is 4.1.22
> Connector version is 5.1.8, JDBC version is documented as 4.0
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6247210.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: Issues with GeneratedValue and SequenceGenerator

Posted by realdepp <th...@freenet.de>.
MySQL version is 4.1.22
Connector version is 5.1.8, JDBC version is documented as 4.0


--
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6247210.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with GeneratedValue and SequenceGenerator

Posted by Jeremy Bauer <te...@gmail.com>.
IIRC, the identity value should be getting returned by the JDBC driver as a
result of the insert.  OpenJPA then populates the entity with that value.
What version of MySQL DB and JDBC driver are you using?

-Jeremy

On Wed, Apr 6, 2011 at 10:16 AM, realdepp <th...@freenet.de> wrote:

> Thanks, but that didn't solve the problem.
>
> EntityManager.refresh() has no effect, like expected.
>
> I'm not enhancing with an eclipse tool, I'm calling the java program via
> ant:
>
>
>
>
>
>
>
>
>
>
> (I also tried the ant task, doesn't seem to make a difference)
>
> However, I get a bunch of warning messages: (I thought ignoring would be ok
> since I get no errors like "class not enhanced")
> openjpa.MetaData - Meta class "x.y.z.TimeSettings_" for entity class
> x.y.z.TimeSettings can not be registered with following exception
> "java.security.PrivilegedActionException: java.lang.ClassNotFoundException:
> x.y.z.TimeSettings_"
>
> I haven't checked yet if the warning message appears for every class, but
> most of them produce it.
>
>
> But I have to correct myself: initialValue AND allocationSize are ignored.
> The IDs start with 1 and they get increased by 50 (default value?) with
> every new persisted entity.
> (And, of course, I don't see any way to get the ids into the application)
>
>
> Is that probably related to MySQL?
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6246413.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: Issues with GeneratedValue and SequenceGenerator

Posted by realdepp <th...@freenet.de>.
Thanks, but that didn't solve the problem.

EntityManager.refresh() has no effect, like expected.

I'm not enhancing with an eclipse tool, I'm calling the java program via
ant:
		 
			
				
				
			
			
			
			
			
		
(I also tried the ant task, doesn't seem to make a difference)

However, I get a bunch of warning messages: (I thought ignoring would be ok
since I get no errors like "class not enhanced")
openjpa.MetaData - Meta class "x.y.z.TimeSettings_" for entity class
x.y.z.TimeSettings can not be registered with following exception
"java.security.PrivilegedActionException: java.lang.ClassNotFoundException:
x.y.z.TimeSettings_"

I haven't checked yet if the warning message appears for every class, but
most of them produce it.


But I have to correct myself: initialValue AND allocationSize are ignored.
The IDs start with 1 and they get increased by 50 (default value?) with
every new persisted entity.
(And, of course, I don't see any way to get the ids into the application)


Is that probably related to MySQL?


--
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-GeneratedValue-and-SequenceGenerator-tp6244055p6246413.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with GeneratedValue and SequenceGenerator

Posted by Michael Dick <mi...@gmail.com>.
Rick's right, my memory was faulty. I think in the earlier versions of
OpenJPA a refresh was required, but when I ran with a 2.1.1 snapshot this
morning I didn't have to call refresh.

Thanks for keeping me honest Rick..

-mike

On Wed, Apr 6, 2011 at 9:45 AM, Rick Curtis <cu...@gmail.com> wrote:

> I'm guessing that this might have to do with your enhancement method? If
> you
> are using the eclipse plugin... that is most likely the problem. See
> OPENJPA-1879[1] for details.
>
> [1] https://issues.apache.org/jira/browse/OPENJPA-1879
>
> Thanks,
> Rick
>
> On Tue, Apr 5, 2011 at 5:36 PM, realdepp <th...@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.
> >
> >
> >
>

Re: Issues with GeneratedValue and SequenceGenerator

Posted by Rick Curtis <cu...@gmail.com>.
I'm guessing that this might have to do with your enhancement method? If you
are using the eclipse plugin... that is most likely the problem. See
OPENJPA-1879[1] for details.

[1] https://issues.apache.org/jira/browse/OPENJPA-1879

Thanks,
Rick

On Tue, Apr 5, 2011 at 5:36 PM, realdepp <th...@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.
>
>
>

Re: Issues with GeneratedValue and SequenceGenerator

Posted by Michael Dick <mi...@gmail.com>.
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 <th...@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.
>