我正在编写一个使用旧数据库的新应用程序。
我有一个名为“分数”的表,如下所示:
+-----+------------+--------------------------+
| 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。
如果Hibernate在“ sub_score”中找到0,如何告诉Hibernate忽略内部Object映射?
试试这个
@JoinColumn(name = "haupt_kondition_id")
@Where(clause = "sub_score <> 0")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句