spring - org.springframework.dao.DuplicateKeyException during delete -


i using spring = 3.2.3.release , hibernate-annotations = 3.5.6-final

i have bean :

@entity public class step implements serializable, comparable<step> { private static final long serialversionuid = -5345848109844492261l;  @id @generatedvalue private integer id;  @column private string description;  @column(nullable = false) private boolean active;  @column(nullable = false) private int steporder;  @column(nullable = false) private string command;  @manytoone @joincolumn(name = "elementid") private element element;  @column private string arg1;  @column private string arg2;  @manytoone @joincolumn(name = "testcaseid", nullable = false) private testcase testcase;  @manytoone @joincolumn(name = "releaseid", nullable = false) private releaseentity releaseentity;  @manytoone @joincolumn(name = "updatedby", nullable = false) private user updatedby;  @temporal(temporaltype.timestamp) @column(columndefinition = "timestamp", insertable = false, updatable = false) @generated(generationtime.always) private date updateddate;  @column(nullable = false) private boolean deleted;  ..... getters/ setters , equals / hashcode 

and have dao persists entity :

@repository("stepdao") @transactional public class stepdaomysql extends basedaomysql implements stepdao { @override public void delete(step step) { if (hashistory(step)) {     list<?> listtodelete = hibernatetemplate.find(         "from stephistory st st.step=?", step);     hibernatetemplate.deleteall(listtodelete); } hibernatetemplate.evict(step); hibernatetemplate.delete(step); }  @override @transactional(readonly = true) public boolean hashistory(step step) { return !collectionutils.isempty(hibernatetemplate.find(     "from stephistory st st.step=?", step)); } 

when try execute delete of dao

org.springframework.dao.duplicatekeyexception: different object same    identifier value associated session: [com.livenation.automation.bean.step#1]; nested exception org.hibernate.nonuniqueobjectexception: different object same identifier value associated session: [com.livenation.automation.bean.step#1] 

actually confused . googled lot duplicate exception , post duplicate exception during saveorupdate nobody face on delete . anyway insert suggested solution - evict method , didn't . bug in mapping exceptions between hibernate , spring ? (i saw such problems before in other projects )

edit : appears problem related execution of method in test context , if execute in production no exception happens test code simple

@test public void candeletestepwithhistory() { linkedstep.setarg1(getrandomstring());    dao.saveorupdate(linkedstep); // in line above change value of linkedstep , dao detect , create appropriate stephistory object linked linkedstep . main goal ofg test check dao can delete such object  dao.delete(linkedstep); step fromdb = dao.getbyid(linkedstep.getid()); assertions.assertthat(fromdb).isnull(); } 

tests executes using @runwith(springjunit4classrunner.class) @contextconfiguration("/test-dispatcher.xml")


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -