【问题标题】:Customizing Hibernate Criteria - Adding conditions to a left join自定义 Hibernate Criteria - 向左连接添加条件
【发布时间】:2011-02-19 09:05:23
【问题描述】:

我需要能够做到以下几点:

从表 1 中选择 * left join Table2 on id1 = id2 AND i1 = ?

休眠条件不允许指定 i1 = ?部分。

现有代码使用休眠标准,换成 HQL 将是一个巨大的重构

有没有人有任何提示我可以如何以不同的方式实现这一点或以任何方式覆盖 Hibernate Criteria?我不反对破解打开hibernate和修改,但是当我开始挖掘它时,似乎有一层又一层的抽象。我从来没有找到实际生成 SQL 的地方......

【问题讨论】:

    标签: hibernate join criteria


    【解决方案1】:

    这似乎是我需要的......

    http://opensource.atlassian.com/projects/hibernate/browse/HHH-2308

    public Criteria createAlias(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException;
    
    public Criteria createCriteria(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException;
    

    【讨论】:

      【解决方案2】:

      您可以使用 Fetch 模式加入。你可以这样做:

      Criteria criteria = session.createCriteria(Table1.class);
      criteria.setFetchMode(table2OutputList, FetchMode, JOIN);
      criteria.add(Restrictions.eq("i1", i1Value);
      return criteria.list();
      

      在这种情况下,您必须在运行此查询之前获取 table2OutputList。这个article可以给你一个更好的主意;

      【讨论】:

      • 那不只是创建这个:SELECT * from Table1 left join Table2 on id1 = id2 WHERE i1 = 吗?我不想要 i1 = 吗?在我希望它作为“ON”表达式的一部分时..
      猜你喜欢
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      相关资源
      最近更新 更多