I am having trouble trying to resolve a problem with an application that uses
an EJB 3 stateless session EJB as a DAO for some JPA entity beans. For one
business rule I need a method in the session EJB to delete existing entity
beans for a submitted key and insert a new set of entity beans passed to the
method. It is possible and likely that some of the new entity beans could
have a primary key that is the same as one of the deleted entities.
I tried this -
MyEntityTestPK e1PK = new e1PK(someValue1, someValue2);
MyEntity1 anEntity = em.find(MyEntity1.class, e1PK);
if ( anEntity != null ) {
em.remove(anEntity);
}
...later in the same method I insert an entity -
MyEntity1 anotherEntity = new MyEntity();
anotherEntity.setPk(e1PK);
anotherEntity.setValue1(somevalue)
em.persist(anotherEntity);
...This produces an exception complaining of a duplicate key. I also tried
doing a merge but that generates a "cannot persist detached object"
exception.
What are the proper steps to delete records then create records again in the
same transaction with (occasionally) the same primary key value?
--
View this message in context: http://www.nabble.com/JPA-with-EJB3---remove-an-entity-and-create-new-entity-with-same-primary-key-%28same-transaction%29-tp21639342p21639342.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.