Announcement Announcement Module
Collapse
No announcement yet.
org.springframework.dao.DuplicateKeyException during delete Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • org.springframework.dao.DuplicateKeyException during delete

    Hi
    I am using
    Spring = 3.2.3.RELEASE
    And Hibernate-annotations = 3.5.6-Final

    I have bean :

    Code:
    @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 I have dao for persists this entity :

    Code:
    @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 where 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 where st.step=?", step));
        }
    When I try to execute delete of my DAO I get
    Code:
    org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [com.livenation.automation.bean.Step#1]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.livenation.automation.bean.Step#1]
    Actually I confused . Googled a lot about duplicate exception and all post is about duplicate exception during saveOrUpdate but nobody face with it on delete . But anyway I insert suggested solution - evict method and didn't help .
    Probably it is some bug in mapping exceptions between hibernate and spring ? (I saw such problems before in other projects )

  • #2
    full exception text - http://pastebin.com/itH3DnKA

    Comment


    • #3
      I think the problem is that step is not associated with the Session. Perhaps you built it from a DTO? See my answer here : http://stackoverflow.com/a/22860855/297331

      Comment

      Working...
      X