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

前端 未结 1 1674
时光取名叫无心
时光取名叫无心 2021-01-25 08:23

I have a one-to-one relation like this

The Parent

@JsonAutoDetect
@Entity
@Table(name = \"Parent\")
public class Parent{

    private Integer id;
    p         


        
相关标签:
1条回答
  • 2021-01-25 08:36

    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.

    0 讨论(0)
提交回复
热议问题