我正在编写一个使用旧数据库的新应用程序。
我有一个名为“score”的表,如下所示:
+-----+------------+--------------------------+
| id | name | sub_score |
+-----+------------+--------------------------+
| 205 | High Score | 206 |
| 206 | Mid Score | 207 |
| 207 | Low Score | 0 |
+-----+------------+--------------------------+
我的模型实体是:
@Entity
@Table(name = "score")
public class ScoreGroups implements Serializable {
private int id;
private String name;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = true)
@JoinColumn(name = "haupt_kondition_id")
@NotFound(action = NotFoundAction.IGNORE)
private ScoreGroups sub_Score;
}
因此,我尝试使用 sub_score 进行自连接映射 id,但由于旧版数据库反设计,如果 Score 没有 sub_score,则写入 0,而不是 null。通过 Hibernate 加载数据会产生:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
因为Hibernate只忽略空值,而不是@NotFound的0。
如果在“sub_score”中发现 0,如何告诉 Hibernate 忽略内部对象映射?
最佳答案
试试这个
@JoinColumn(name = "haupt_kondition_id")
@Where(clause = "sub_score <> 0")
关于java - 我如何告诉 Hibernate 在自连接 @OneToOne 中忽略 "0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56039763/