如果list属性为空,则获取实体

时间:2014-07-24 15:48:59

标签: java hibernate

我有以下模特:

@Entity
@Table(name = "feature")
public class DbFeature {

    @Id
    @GeneratedValue
    public Long id;

    @ManyToOne
    public DbProduct product;
}

@Entity
@Table(name = "product")
public class DbProduct {

    @Id
    @GeneratedValue
    public Long id;

    @OneToMany(mappedBy = "product")
    public List<DbFeature> features;
}

如何制作Criteria查询哪些产品会返回所有没有功能的产品?

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个。

Criteria criteria = session.createCriteria(DbProduct.class).createAlias("features", "feature", CriteriaSpecification.LEFT_JOIN).add(Restrictions.isNull("feature"));

注意:它没有经过测试。