Hibernate删除一条记录,但记录不会消失? [英] Hibernate deletes a record, but the record doesn't go away?
问题描述
//从永久存储区和本地列表中删除
int index = get_record_to_delete();如果
(entityList.getEntities()得到(指数).getEntityId()!= NULL){
//现有坚持记录
entityService.delete(entityInfo.getEntities()得到(指数).getEntityId());
}
//从本地列表中删除
entityList.getEntities()。remove(index);
返回所有者信息;
//由上述代码调用的entityService
public void delete(MyEntity entity){
repository.delete(entity); // org.springframework.data.jpa.repository.JpaRepository的子类
}
I使用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)
私人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,nullable = false,precision = 3,scale = 0)
private Integer myOther;
@ManyToOne
@JoinColumn(name =PARENT_ID,nullable = false)
私人MyParent父母;
@Column(name =PHONE,nullable = false,precision = 10,scale = 0)
private myPhone;
@Column(name =SOCIAL,nullable = false,length = 9)
private String mySocial;
}
任何人都可以看到任何可能暗示为什么这个实体不会被删除的东西? p>
此原来是一个Hibernate @OneToMany注释取消该删除的情况。
由于父对象上有一个 @OneToMany(cascade = CascadeType.ALL)
注解,所以如下:
类MyParent {
@OneToMany(cascade = CascadeType.ALL)
Set< MyObject> myObjects;
Hibernate显然遍历整个对象图,出于某种原因,由于父母未被删除,因此取消了子删除。
检测这件事的方法是将您的日志记录一直变为 TRACE
并查找以下内容来自Hibernate的消息:
取消排程实体删除
这些是让你知道Hibernate取消你的删除的神奇单词。这没有特别好记录,所以我希望这可以帮助别人。
EDIT: Oops, I guess it would help if I posted the delete code. Sorry.
// 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
}
I'm using Spring Data/JPA V 1.4.2.RELEASE and hibernate 3.6.10.Final.
I have a situation where I'm deleting an entity, and delete appears to work, but when I refresh my web page, the entity reappears. So, it doesn't produce an error, but the record doesn't get deleted.
This entity has child entities contained within it. I suspect that some kind of entity dependence problem is keeping the entity from being truly "deleted", but I don't know which entity dependency it might be, and Hibernate's not telling me. It doesn't matter if the contained entities get deleted or not, but this entity needs to get deleted. Not sure why that's not happening. Here's the entity I'm trying to delete:
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", nullable = false, precision = 3, scale = 0)
private Integer myOther;
@ManyToOne
@JoinColumn(name = "PARENT_ID", nullable = false)
private MyParent parent;
@Column(name = "PHONE", nullable = false, precision = 10, scale = 0)
private Long myPhone;
@Column(name = "SOCIAL", nullable = false, length = 9)
private String mySocial;
}
Anyone see anything that might suggest why this entity won't delete?
This turned out to be a case of a Hibernate @OneToMany annotation canceling the delete.
Because the parent object had a @OneToMany(cascade=CascadeType.ALL)
annotation on it, like this:
Class MyParent {
@OneToMany(cascade=CascadeType.ALL)
Set<MyObject> myObjects;
}
Hibernate apparently walks the entire object graph and for some reason, because this parent wasn't being deleted, it canceled the child delete.
The thing to do to detect this is turn your logging all the way up to TRACE
and look for the following message from Hibernate:
un-scheduling entity deletion
Those are the magic words that let you know that Hibernate is canceling your delete. This was not particularly well documented, so I hope this helps someone else.
这篇关于Hibernate删除一条记录,但记录不会消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!