【问题标题】:DetatchedCriteria without on clause没有 on 子句的 DetachedCriteria
【发布时间】:2013-08-26 14:34:54
【问题描述】:

我对 Hibernate 比较陌生。我必须使用 3.2 版本,我需要使用 DetachedCriteria 并获取以下查询:

select this_.ID as ID0_1_, this_.SNDG as SNDG0_1_
, this_.NDG as NDG0_1_, this_.T_GWR_PARTNER_ID as T4_0_1_
, table2x1_.ID as ID1_0_, table2x1_.T_GWR_PROPOSAL_ID as T2_1_0_
, table2x1_.GROUP_SNDG as GROUP3_1_0_, table2x1_.GROUP_NAME as GROUP4_1_0_
from t_gwr_proposals this_ 
inner join 
t_gwr_proposal_ratings table2x1_ 
where table2x1_.T_GWR_PROPOSAL_ID=this_.ID

但我得到了以下内容

select this_.ID as ID0_1_, this_.SNDG as SNDG0_1_
, this_.NDG as NDG0_1_, this_.T_GWR_PARTNER_ID as T4_0_1_
, table2x1_.ID as ID1_0_, table2x1_.T_GWR_PROPOSAL_ID as T2_1_0_
, table2x1_.GROUP_SNDG as GROUP3_1_0_, table2x1_.GROUP_NAME as GROUP4_1_0_ 
from t_gwr_proposals this_ 
inner join t_gwr_proposal_ratings table2x1_ 
** on this_.ID=table2x1_.ID ** 
where table2x1_.T_GWR_PROPOSAL_ID=this_.ID

使用此代码:

Criteria c = session.createCriteria(T_gwr_proposals.class, "Table1");
c.createAlias("Table1.T_gwr_proposal_ratings", "Table2"); // inner join by default
c.add(Restrictions.eqProperty("Table2.t_gwr_proposal_id", "Table1.proposalsId"));
return c.list();

有人可以帮帮我吗?

非常感谢,

托马索 A.

【问题讨论】:

    标签: mysql sql hibernate join detachedcriteria


    【解决方案1】:

    条件不适用于表格,但适用于实体及其关联。您只能通过它们之间存在的关联加入两个实体。并且条件查询中只能存在一个根实体。因此,除非在使用 table2x1_.T_GWR_PROPOSAL_ID=this_.ID 作为映射的实体之间存在关联,否则您将无法在 Criteria 中创建这样的查询(不过 HQL 应该可以做到)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      相关资源
      最近更新 更多