Hibernate Criteria Return parent record that have one-to-one child record not null?

北城余情 提交于 2019-12-02 07:49:57
JB Nizet

First of all, the mapping is wrong. In the parent class, you're saying that the association is mapped by child.parent, and immediately after you're saying that it's mapped using a join column named id_child. Make up your mind. Either it's mapped by the child.parent property, and you should remove the @JoinColumn. Or it's mapped by the JoinColumn and you should remove the @PrimaryKeyJoinColumn in the child, and use mappedBy="child" in the Child entity.

Now, to make your query work, whatever the mapping is, you should simply make an inner join:

Criteria criteria = session.createCriteria(Parent.class);
criteria.createAlias("child"); // inner join

Since the join is an inner join, it will only select parents that have a non-null child.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!