You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by tightrope <ro...@thingsprime.com> on 2009/01/11 22:27:55 UTC
Generated primary key values are available in the PostPersist
method ?
Help. This should be easy
The subject header for this post contains a quote from the JPA spec., but I
don't get ids (created using @GeneratedValue annotation on id field) until
commit time.
I thought maybe it was to do with the runtime enhancement, so I switched to
build-time. No change. I also tried different databases - Derby, Mysql,
Postgres. No luck.
This seems a basic issue. Can anyone help demystify? Confirm that they get
generated ids before commit, tell me what I'm misunderstanding, etc?
Thanks.
--
View this message in context: http://n2.nabble.com/Generated-primary-key-values-are-available-in-the-PostPersist-method---tp2142933p2142933.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Generated primary key values are available in the PostPersist
method ?
Posted by tightrope <ro...@thingsprime.com>.
Hi, I tried also a simple example and saw it working...
The problem arose when I was trying to use Compass with OpenJPA. It has
embedded support for OpenJPA, hooking on to lifecycle events to
transparently index persisted information.
A Compass unit test uses an entity class with a manually set id. When I
changed the annotation in the Entity class to GeneratedValue, and commented
out the manual id setting, the test failed. I opened a thread on Compass
Support
(http://forum.compass-project.org/thread.jspa?threadID=215882&tstart=15).
Shay Banon said Compass was hooking into PostPersist event.
Looking at it again, it appears that maybe the mistaken assumption is in
Compass (that PostPersist is called after em.persist?), or else I have made
some mistake in configuration. Apologies for wasting your time if that is
the case.
In case you are interested, here is the test class:
http://svn.compass-project.org/svn/compass/trunk/src/main/test/org/compass/gps/device/jpa/openjpa/OpenJPASimpleJpaGpsDeviceTests.java
which inherits a simple test from
http://svn.compass-project.org/svn/compass/trunk/src/main/test/org/compass/gps/device/jpa/AbstractSimpleJpaGpsDeviceTests.java
which uses this entity class "Simple":
http://svn.compass-project.org/svn/compass/trunk/src/main/test/org/compass/gps/device/jpa/model/Simple.java
However, what's vital to know is not apparent there (where/how exactly the
compass code hooks in). maybe you will know how that must be? Anyway, I will
keep trying to figure it out. Thanks for your responses.
--
View this message in context: http://n2.nabble.com/Generated-primary-key-values-are-available-in-the-PostPersist-method---tp2142933p2147480.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Generated primary key values are available in the PostPersist method ?
Posted by Dinkar Rao <di...@gmail.com>.
Hi,
>> Do you know of a unit test or example where this is tested - whether a
>> PostPersist method has access to a generated id value?
I didn't find any that explicitly tested the id value, but there is
one, TestMessageListeners.java, that uses PostPersist callbacks. I
put breakpoints in the PostPersist methods of the listeners and
checked out the object passed in. The id is set as expected everytime.
This was with trunk.
A testcase showing failures would definitely help.
Thanks,
Dinkar
Re: Generated primary key values are available in the PostPersist
method ?
Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
> I observe is the PostPersist method being called with the id having
value of zero
PostPersist callback should be invoked after persistent identity value has
been assigned. Please post the entity class, the listener class (if
@PostPersist is not on the entity class itself) and the application code
that invokes persist().
As Dinkar pointed out that OpenJPA calls back post-persist after
flush/commit and not just immediately after persist() has been called. But I
think the application should be able to configure this behavior on when
post-persist callback is invoked.
--
View this message in context: http://n2.nabble.com/Generated-primary-key-values-are-available-in-the-PostPersist-method---tp2142933p2146434.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Generated primary key values are available in the PostPersist
method ?
Posted by tightrope <ro...@thingsprime.com>.
Thanks Dinkar,
but what I observe is the PostPersist method being called with the id having
value of zero (i.e. not having the generated value, i.e. before commit).
Do you know of a unit test or example where this is tested - whether a
PostPersist method has access to a generated id value?
--
View this message in context: http://n2.nabble.com/Generated-primary-key-values-are-available-in-the-PostPersist-method---tp2142933p2144925.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Generated primary key values are available in the PostPersist method ?
Posted by Dinkar Rao <di...@gmail.com>.
Hi,
The PostPersist method does have access to the generated id value. But
the id value is generated when a new record is inserted into the
table. It is not guaranteed that the PostPersist method will be called
immediately after an em.persist() call. The spec says,
"The PostPersist and PostRemove methods will be invoked after the
database insert and delete operations respectively."
These insert and delete operations may themselves happen only at
em.flush() or em.commit() time. At that time, the id value is
generated and the PostPersist method is called.
Thanks,
Dinkar