休眠条件返回具有一对一子记录的父记录不为null? [英] Hibernate Criteria Return parent record that have one-to-one child record not null?

查看:80
本文介绍了休眠条件返回具有一对一子记录的父记录不为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的一对一关系

父母

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

    private Integer id;
    private Child child;

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column (name="idparent")
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
    @JoinColumn(name="idchild", nullable=true)
    public Child getChild() {
        return child;
    }
    public void setChild(Child child) {
        child.setParent(this);
        this.child = child;
    }
}

和孩子

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

    private Integer id;
    private Parent parent;

    @Id
    @GeneratedValue(generator = "foreign")
    @GenericGenerator(name = "foreign", strategy = "foreign", parameters = { @Parameter(name = "property", value = "parent") })
    @Column (name = "idchild")
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    @OneToOne(fetch = FetchType.LAZY, optional = true)
    @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
    @PrimaryKeyJoinColumn
    @JsonIgnore
    public Parent getParent() {
        return parent;
    }
    public void setParent(Parent parent) {
        this.parent= parent;
    }

}

我想创建返回具有孩子isNotNull的父母的条件,我尝试过这样的想法

I want to create criteria that return the parent that have child isNotNull, i tryed somme think like

Criteria criteria = session.createCriteria(Parent.class);
criteria.add(Restrictions.isNotNull("child"));

但是不行,请给我个榜样可以帮助我吗?谢谢

But not work, can you help me by giving somme example please ? Thanks

推荐答案

首先,映射错误.在父类中,您说的是该关联是由child.parent映射的,而在您说该关联是使用名为id_child的连接列进行映射之后.下定决心.要么由child.parent属性映射,您都应删除@JoinColumn.或者它是由JoinColumn映射的,您应该删除子级中的@PrimaryKeyJoinColumn,并在子级实体中使用mappedBy="child".

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.

这篇关于休眠条件返回具有一对一子记录的父记录不为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆