我对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.
最佳答案
条件不适用于表格,但适用于实体及其关联。您只能通过两个实体之间存在的关联来加入它们。条件查询中只能存在一个根实体。因此,除非使用table2x1_.T_GWR_PROPOSAL_ID = this_.ID作为其映射的实体之间存在关联,否则您将无法在Criteria中创建这样的查询(尽管HQL应该可以完成)。
关于mysql - 没有on子句的DetachedCriteria,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18404566/