8

我有一些逻辑删除的记录(即active=false)导致我的@ManyToOne映射出现问题,因为连接列返回了多个结果。

我只需要包含active=true我认为可以通过以下方式实现的记录:

@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "site_id", referencedColumnName = "site_id", insertable = false, updatable = false)
@WhereJoinTable(clause = "active=true")
private Site site;

但是,hibernate 似乎WhereJoinTable没有使用 (也许它只对OneToMany? 有效),因为active=true没有出现在生成的 SQL(日志)中并且问题仍然存在。

是否可以包含用于连接 a 的 where 子句ManyToOne以及如何连接?

4

3 回答 3

4

@ManyToOne 不支持 @WhereJoinTable。自五年以来,关于主题开放的错误HHH-4335 。我不知道任何解决方法,除了使用错误报告中提到的视图(在只读访问的情况下)。

于 2012-04-30T02:46:00.777 回答
1

@JoinColumnOrFormula注释是一种合适的解决方法

于 2017-10-20T01:14:50.040 回答
0

对我有用的解决方案是在类的顶部添加 @where 注释,请参见下一个示例:

@Where(clause = "state_.idCity=0")
@SuppressWarnings("serial")
@Entity
@Table(name="city", schema="catalog")
@SequenceGenerator(name = "default_gen", sequenceName = "IDSERIEINVALIDO", allocationSize = 1)
public class Citye implements Serializable{
        
    @Id 
    private Long id;
}
于 2021-03-03T13:35:54.397 回答