java - Resolving the JPA/Hibernate EntityNotFoundException -
i've run weird situation here. when try delete set of entities & later try add set of entities, may or may not have same elements again,i seem getting below exception. whole stack trace isn't required doesn't much.
javax.persistence.entitynotfoundexception: deleted entity passed persist [com.test.myentity#<null>]
to make myself more clear, let me give more detailed explanation.
this entity.
@entity @table(name = "my_entity") @sequencegenerator(name = "my_enty_seq", sequencename = "my_entity_seq") public class myentity { private long id; @id @generatedvalue(strategy = generationtype.auto, generator = "my_enty_seq") @column(name = "id", unique = true, nullable = false, precision = 12, scale = 0) public long getid() { return this.id; } // other methods & variables not relevant here. }
and class playing entities.
public class myentityserviceimpl implements myentityservice { public void updatemyentities(list<myentity> newentities) { // delete old items list<myentity> oldentities = mydao.getallentities(); mydao.delete(oldentities); // mydao extends genericdaojpawithoutjpatemplate, hence, delete available in it. // add new items mydao.store(newentities); // mentioned exception. } // other non-relevant stuffs }
the scenario mentioned exception on store()
method when there common myentity
object, present in both oldentities , newentities lists.
i've tried quite few things make work, nothing me overcome problem. adding fetchtype.eager
& cascasetype.all, cascasetype.persist
didn't work. jpa entitymanager
doesn't seem have commit()
method either, use after delete()
& before store()
method. i've tried interchanging store()
persist()
(though store internally calls persist()
) too!
note:- requirement delete old list , insert fresh list, comparing elements , adding new ones tedious(the list can pretty huge @ times).
the exception quite clear think: told session delete entity x , @ same time store entity x. poor thing confused , doesn't know do.
do session.flush()
possibly via commit
before persisting new/old/deleted entity.
edit:
if doesn't trick, need insert in new session. problem might still be, don't have transient entity, 1 deleted. might have copy data new instance.
Comments
Post a Comment