On 11-02-24 02:40 PM, ses wrote:
> On Feb 24, 6:35 pm, Daniele Futtorovic<da.futt.n...@laposte-dot-
> net.invalid> wrote:
>> On 24/02/2011 15:11, ses allegedly wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> I assume this must be a fairly common task but I'm struggling to find
>>> the best way to achieve this.
>>
>>> How do you best retrieve the entity you've just persisted with
>>> entityManager.persist(myEntity), so that you can then use it in the
>>> next part of a transaction.
>>
>>> I'm referring to an entity for which the container or database manages
>>> a generated primary key. Therefore you want to retrieve what you've
>>> just persisted so that you can for example establish another
>>> relationship - e.g. anotherEntity.add(myEntity) then
>>> entityManager.merge(anotherEntity).
>>
>>> I am trying to do this in a session bean method and carry it out all
>>> as one transaction. The way I am retrieving myEntity after persisting
>>> is simply selecting the entity with the highest key value (is simple
>>> auto increment in MySQL) but there is some problem with this. It
>>> retrieves the entity fine with its newly generated key, however when I
>>> try to merge it or merge another entity with which I establish a
>>> relationship to myEntity I get the following exception:
>>
>>> "null primary key encountered in unit of work"
>>
>>> Any help would be much appreciated, I'm really asking if there is a
>>> better way to do this (persist-find-merge in a transactional EJB
>>> session bean method doesn't seem to be a good pattern, or at least
>>> doesn't seem to be working for me!)
>>
>> Not sure about EJB, but in pure Hibernate I'd use #merge(Object) for
>> this -- and then work on the returned instance.
>>
>> HTH,
>>
>> --
>> DF.
>
> I don't think you can merge in JPA before you've called persist, or do
> you mean call merge after persist?
For a good discussion, see
http://blog.xebia.com/2009/03/23/jpa...ched-entities/.
I particularly recommend "The Pattern" section at the bottom. I'd reword
the second piece of advice as
"When updating an existing *managed* entity, we do not invoke any
EntityManager method; the JPA provider will automatically update the
database at flush or commit time."
This ties in better with the third guideline. Main point is, and this is
something that practically all of us have done when starting out with
JPA, you don't need to call merge() or persist() to "save" changes to a
managed entity. That's one of the main reasons we've got the EntityManager.
The persistence specifications (JSR 220 and/or JSR 317) are your
authoritative references. They are very readable specs.
I'm not sure I understand your specific problem. You're in the same
transaction - there's no way in your code of passing the newly-persisted
and now managed entity to the next spot that needs to do something with
it? What I'm saying is, I don't get the "find" part of
persist-find-merge...in fact I'm not even convinced you need the "merge"
in your scenario.
As an aside, it seems you're using IDENTITY as your ID generation
strategy. Is that so?
AHS
--
We must recognize the chief characteristic of the modern era - a
permanent state of what I call violent peace.
-- James D. Watkins