实体A和B使用链接表AtoB具有多对多关系。
如果实体A被删除,相关的链接也会被hibernate删除。到目前为止一切顺利。
我的问题是,我的链接表是一个隐藏了更复杂关系的 View ,并且在这种情况下工作得很好,除非 hiberate 尝试从 View 中删除链接行,从而导致数据库提示。
@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;
}
有没有办法让 hibernate 不删除链接行?我还没有找到任何级联选项或在关联上使用 updateable=false
等的能力。
最佳答案
我遇到了这个问题,以下方法对我有用:
@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;
}
}
关于java - 删除hibernate实体而不(尝试)删除关联表( View )条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1763863/