Hibernate HQL中的多个联接条件,同时映射数据而不是where子句

安库什·桑赫扬

我正在尝试做这样的事情

本机SQL:

Select * 
from FirstTable ft 
   right join SecondTable st on (st.ID=ft.ID and ft.fStatus !='b') where ft.fStatus!='b'

总之我只是不想地图FirstTable数据与SecondTable当有这样的事情[ft.fStatus !='b']但是,发生这种情况时,我想从中获得相应的记录SecondTable

我搜索了此with关键字并找到了关键字,但这对我的情况没有帮助。请查看下面的示例数据以获取任何参考:

FirstTable
   fId  |   fName   |   fStatus
++++++++++++++++++++++++++++++
    1   |   Name1   |     b
    2   |   Name2   |     b
    3   |   Name3   |     a
    2   |   Name4   |     b
    3   |   Name5   |     b
    4   |   Name6   |     a
    4   |   Name7   |     a


SecondTable
   sId  |   sName
+++++++++++++++++++
    1   |   Name1
    2   |   Name2
    3   |   Name3
    4   |   Name4


Expected Output :

   fId   |   fName    |   sId  |   sName   
++++++++++++++++++++++++++++++++++++++++++++
    _   |     _       |    1   |   Name1
    _   |     _       |    2   |   Name2
    3   |   Name3     |    3   |   Name3
    4   |   Name6     |    4   |   Name4
    4   |   Name7     |    4   |   Name4
安库什·桑赫扬

您需要oneToManyParent Entityie中SecondTableChild Entityie添加映射FirstTable例如 :

class FirstTable{
    Integer pkColumn;
    SecondTable fId ; 
    String fName ;
    String  fStatus;

    --Getter and Setters
}

class SecondTable{
    Integer sId ; 
    String sName ;

    @oneToManny(mappedBy="fId ")
    List<FirstTable> firstTable;

    --Getter and Setters
}

HQL为您的预期输出:

SELECT ft.fId, ft.fName, st.sId, st.sName FROM SecondTable st 
  LEFT JOIN st.firstTable ft WITH ft.fStatus != 'b' 
    WHERE (--other Conditions--)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章