如何在Hibernate中选择数组大小= 0的对象?

时间:2015-03-20 14:08:10

标签: hibernate

我正在使用Hibernate,我有类似的东西:

public class Product{
  private Integer id;
  private List<Price> prices;

  // getters and setters
}

public class Price{
  private Integer id;
  private Double price;
  private Product product;
  private List<Recipe> recipes;

  // getters and setters
}

public class Recipe{
  private Integer q;

  //getters and setters
}

我想知道我是否可以做类似的事情:

List<Product> products = session.createQuery(
    "select product from Price p"
    + " inner join p.recipes where p.recipes = null ").list();

我想从没有任何食谱的价格中选择所有产品

1 个答案:

答案 0 :(得分:0)

session.createCriteria(Product.class).createAlias("prices", "prices")
            .createAlias("prices.recipes", "recipes", Criteria.LEFT_JOIN)
            .add(Restrictions.isNull("recipes.id"))
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).list();