我正在使用 Spring = 3.2.3.RELEASE 而Hibernate-annotations = 3.5.6-Final
我有豆:
@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
我坚持这个实体:
@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));
}
当我尝试删除我的DAO时,我得到了
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]
其实我很困惑。用Google搜索了很多关于重复异常的内容,所有帖子都是关于saveOrUpdate期间的重复异常但没有人在删除时面对它。但无论如何我插入建议的解决方案 - 逐出方法并没有帮助。 可能是在映射hibernate和spring之间的异常时会出现一些错误? (我之前在其他项目中看到了这些问题)
编辑:看来问题只与在测试环境中执行此方法有关,如果在生产中执行它,则不会发生异常 测试代码非常简单
@Test
public void canDeleteStepWithHistory()
{
linkedStep.setArg1(getRandomString());
dao.saveOrUpdate(linkedStep); // in line above we change value of linkedStep , DAO will detect this and will create appropriate StepHistory object that linked with linkedStep . This is main goal ofg test check that DAO can delete such object
dao.delete(linkedStep);
Step fromDB = dao.getById(linkedStep.getId());
Assertions.assertThat(fromDB).isNull();
}
使用@RunWith(SpringJUnit4ClassRunner.class)执行测试 @ContextConfiguration( “/测试dispatcher.xml”)