删除休眠实体而不(尝试)删除关联表(视图)条目 [英] delete hibernate entity without (attempting to) delete association table (view) entry
问题描述
实体 A 和 B 使用链接表 AtoB 具有多对多关系.
Entity A and B have a many to many relationship using link table AtoB.
如果Entity A被删除,hibernate会删除相关链接.到目前为止一切顺利.
If Entity A is deleted, the related links are deleted by hibernate. So far so good.
我的问题是我的链接表是一个隐藏了更复杂关系的视图,并且在这种情况下可以完美运行,除非 hiberate 尝试从视图中删除链接行,导致数据库抱怨.
My problem is that my link table is a view hiding a much more complicated relationship and works perfectly in this situation except when hiberate tries to delete the link rows from the view, causing the database to complain.
@Entity A...
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "AtoB",
joinColumns = @JoinColumn(name = "A_ID"),
inverseJoinColumns = @JoinColumn(name = "B_ID"))
public Set<A> getASet() {
return ASet;
}
有没有办法让休眠不删除链接行?我还没有找到任何级联选项或在关联上使用 updateable=false
等的能力.
Is there a way to get hibernate to not delete the link rows? I haven't found any cascade options or the ability to use updateable=false
etc on an association.
推荐答案
我遇到了这个问题,以下对我有用:
I encountered this issue and the following worked for me:
@ManyToMany
@JoinTable(name = "V_LoanSecuredUser",
joinColumns = @JoinColumn(name = "loanAdditionalInfo_id", updatable = false),
inverseJoinColumns = @JoinColumn(name = "userAuthentication_Id", updatable = false))
@Persister(impl = ReadOnlyCollectionPersister.class)
@Immutable
public class ReadOnlyCollectionPersister extends BasicCollectionPersister {
public ReadOnlyCollectionPersister(Collection collection,
CacheConcurrencyStrategy cache, Configuration cfg,
SessionFactoryImplementor factory) throws MappingException,
CacheException {
super(collection, cache, cfg, factory);
}
@Override
protected boolean isRowDeleteEnabled() {
return false;
}
@Override
protected boolean isRowInsertEnabled() {
return false;
}
}
这篇关于删除休眠实体而不(尝试)删除关联表(视图)条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!