S
ses
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!)
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!)