将SQL Join转换为HQL Join:多个连接,一个表,不加入pk

时间:2015-11-15 22:44:13

标签: hibernate hql

我搜索了SO,但没有找到一个很好的例子。我有以下SQL语句可以正常工作:

select CatA.* 
FROM TestContentElementCETPropertyValue as CatA 
  JOIN TestContentElementCETPropertyValue as CatB 
    ON CatA.testcontentelementid = CatB.testcontentelementid 
  JOIN TestContentElementCETPropertyValue as CatC
    ON CatB.testcontentelementid = CatC.testcontentelementid 
WHERE  CatA.contentelementtypepropertyid = 141 
  AND CatB.contentelementtypepropertyid = 4903   
  AND CatC.contentelementtypepropertyid = 4990

更新:要清楚,ON字段或WHERE字段都不是PK。

作为HQL的初学者,一旦“ON”条款消失,我就很难理解如何写这个。

1 个答案:

答案 0 :(得分:0)

我明白了。联接是隐含的。

String queryString="        select CatA FROM "   +
    "TestContentElementCETPropertyValue as CatA, "+
    "TestContentElementCETPropertyValue as CatB, "+
    "TestContentElementCETPropertyValue as CatC "+
    "WHERE  CatA.contentElementTypeProperty.id = 141 "+
    "AND CatB.contentElementTypeProperty.id= 4903   "+
    "AND CatC.contentElementTypeProperty.id = 4990  "+
    "AND CatA.testContentElement.id = CatB.testContentElement.id "+
    "AND CatB.testContentElement.id = CatC.testContentElement.id ";