// delete from permanent store and local list
int index = get_record_to_delete();
if (entityList.getEntities().get(index).getEntityId() != null ) {
// existing persisted record
entityService.delete(entityInfo.getEntities().get(index).getEntityId());
}
//remove from local list
entityList.getEntities().remove(index);
return ownersInfo;
// entityService called by above code
public void delete(MyEntity entity) {
repository.delete(entity); // subclass of org.springframework.data.jpa.repository.JpaRepository
}
我正在使用Spring Data / JPA V 1.4.2.RELEASE和hibernate 3.6.10.Final.
我有一种情况,我正在删除一个实体,删除似乎工作,但当我刷新我的网页时,实体重新出现.因此,它不会产生错误,但记录不会被删除.
该实体包含其中的子实体.我怀疑某种实体依赖问题是让实体不被真正“删除”,但我不知道它可能是哪种实体依赖,而Hibernate没有告诉我.包含的实体是否被删除无关紧要,但需要删除此实体.不知道为什么没有发生这种情况.这是我要删除的实体:
public class MyEntity implements java.io.Serializable {
@Id
@GeneratedValue
@Column(name = "MYID",unique = true,nullable = false)
private Long myId;
@Column(name = "MY_NAME",nullable = false,length = 50)
private String myName;
@ManyToOne
@JoinColumn(name = "ADDRESS_ID",nullable = false)
private AddressEntity myAddress;
@ManyToOne
@JoinColumn(name = "OTHER_ADDRESS_ID",nullable = true)
private AddressEntity myOtherAddress;
@ManyToOne
@JoinColumn(name = "TYPE_CODE",nullable = false)
private MyType myType;
@ManyToOne
@JoinColumn(name = "ADDRESS_CODE",nullable = false)
private AddressCode addressCode;
@Column(name = "OTHER",precision = 3,scale = 0)
private Integer myOther;
@ManyToOne
@JoinColumn(name = "PARENT_ID",nullable = false)
private MyParent parent;
@Column(name = "PHONE",precision = 10,scale = 0)
private Long myPhone;
@Column(name = "SOCIAL",length = 9)
private String mySocial;
}
任何人都可以看到任何可能暗示为什么这个实体不会删除?
最佳答案
结果证明是Hibernate @OneToMany注释取消删除的情况.
原文链接:https://www.f2er.com/spring/431947.html因为父对象上有一个@OneToMany(cascade = CascadeType.ALL)注释,如下所示:
Class MyParent {
@OneToMany(cascade=CascadeType.ALL)
Set
Hibernate显然遍历整个对象图,由于某种原因,因为这个父级没有被删除,所以它取消了子删除.
要检测到这一点的方法是将日志记录一直转到TRACE并从Hibernate查找以下消息:
un-scheduling entity deletion
这些是让你知道Hibernate取消删除的神奇词汇.这没有特别好记录,所以我希望这有助于其他人.