如何使用hibernate标准实现使用内部联接对象选择查询 [英] How to implement with hibernate criteria Object the select query with inner join

查看:93
本文介绍了如何使用hibernate标准实现使用内部联接对象选择查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate和Criteria Query的新手。
所以我在HQL中实现了这个查询:

$ p $ select A.mobilephone
B.userNick
C.creditCard
from mobile_table A
内部连接user_table B
A.codmobile = B.codmobile
内部连接信用C
A.mobileCredit = C .mobileCredit

如何使用Hibernate Criteria Object实现它?


<您的示例只是一个本机SQL,而不是HQL。无论如何,您可以使用标准 API来构建所需的Criteria对象:





例如,如果SQL为:

  select 
TableA.columnA,TableB.columnB,TableC.columnC $ b $ TableB
内连接TableB TableA.tableB_id = TableB.id
内连接TableC on TableA.tableC_id = TableC。 id
其中TableA.columnAA =AAA
和TableB.columnBB =BBB
和TableC.columnCC =CCC

然后Criteria对象将会是:

  List< Object []> resultList =(List< Object []>)session.createCriteria(TableA.class,aliasOfTableA)
.add(Restrictions.eq(aliasOfTableA.columnAA,AAA))
.createCriteria(aliasOfTableA.tableBId,aliasOfTableB)
.add(Restrictions.eq(aliasOfTableB.columnBB,BBB))
.createCriteria(aliasOfTableA.tableCId ,aliasOfTableC)
.add(Restrictions.eq(aliasOfTableC.columnCC,CCC))
.setProjection(Projections.projectionList()
.add(Projections.property( aliasOfTableA.columnA))
.add(Projections.property(aliasOfTableB.columnB))
.add(Projections.property(aliasOfTableC.colu mnC))
).list();

for(Object [] obj:resultList){
System.out.println(obj [0]); //打印来自TableA.columnA的值
System.out.println(obj [1]); //打印TableB.columnB的值
System.out.println(obj [2]); //打印来自TableC.columnC的值
}

请注意, Criteria 在映射的Java实体中使用属性名称和类名称。


I'm new with Hibernate and Criteria Query. So I have implemented the query in HQL:

select A.mobilephone
    B.userNick
    C.creditCard
from mobile_table A
inner join user_table B
    on A.codmobile=B.codmobile
inner join Credit C 
    on A.mobileCredit= C.mobileCredit

How can I implement it with Hibernate Criteria Object?

解决方案

Your example is just a native SQL , not the HQL . Anyway , you can use the following methods from the Criteria API to construct the desired Criteria object :

For example , if the SQL is :

select
    TableA.columnA ,TableB.columnB ,TableC.columnC
from TableA 
inner join TableB on TableA.tableB_id=TableB.id
inner join TableC on TableA.tableC_id=TableC.id
where TableA.columnAA="AAA"
and TableB.columnBB="BBB"
and TableC.columnCC="CCC"

Then the Criteria object will be:

List<Object[]>resultList= (List<Object[]>)session.createCriteria(TableA.class, "aliasOfTableA") 
                .add(Restrictions.eq("aliasOfTableA.columnAA", "AAA"))  
                .createCriteria("aliasOfTableA.tableBId" , "aliasOfTableB")
                .add(Restrictions.eq("aliasOfTableB.columnBB", "BBB"))
                .createCriteria("aliasOfTableA.tableCId" , "aliasOfTableC")
                .add(Restrictions.eq("aliasOfTableC.columnCC", "CCC"))
                .setProjection( Projections.projectionList()
                        .add( Projections.property("aliasOfTableA.columnA") )
                        .add( Projections.property("aliasOfTableB.columnB"))
                        .add( Projections.property("aliasOfTableC.columnC") )
                ).list();

for (Object[] obj : resultList) {
        System.out.println(obj[0]); //print the value from TableA.columnA
        System.out.println(obj[1]); //print the value from TableB.columnB
        System.out.println(obj[2]); //print the value from TableC.columnC
}   

Please note that all parameters in the Criteria use the property name and class name in the mapped Java entities.

这篇关于如何使用hibernate标准实现使用内部联接对象选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆