5

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?

Tom11
  • 2,419
  • 8
  • 30
  • 56
Bomberlatinos9
  • 790
  • 3
  • 11
  • 24

2 Answers2

9

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.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • than you Very much....I think your suggestion are very so close to what i would to obtain...So only need to try the solution. I have a question: if i would insert a restriction i have to put it in projections?? tanx for all – Bomberlatinos9 Nov 17 '11 at 10:34
  • Good solution but, Sorry, I have an Stupid DOubt: in the code above maybe do you intend do write .createCriteria("aliasOfTableA.tableBId" , "aliasOfTableB"), and aliasofTableA (refers at the line above?) instead of .createCriteria("AliasOfTableA.tableBId" , "aliasOfTableB") – Bomberlatinos9 Nov 17 '11 at 14:24
  • Yes , you are right . Typed wrong before , i just corrected it – Ken Chan Nov 17 '11 at 14:41
  • Just a bit confused here, how does 'aliasOfTableB' denotes TableB here? – Yashasvi Raj Pant Mar 13 '20 at 11:53
0

Are you sure that's HQL? Looks like native SQL to me.

In order to use Criteria your entities need to be mapped and associated with each other (since Criteria only allows navigating association paths). I think you could get more help if you give some details about your mappings.

Vlad
  • 10,602
  • 2
  • 36
  • 38
  • Yes it is in native SQL, because i prefer to write it on thi way. So the association mappings i think is not interesting, but how i can translate a sql native wrote above in Hibernate Criteria, because I need to understand how i can do it. Have you an idea? Looking inside java-stack i have found the link: http://stackoverflow.com/questions/2252468/hibernate-criteria-and-multiple-join but i don't understand some parts of it... Who could help me?? – Bomberlatinos9 Nov 17 '11 at 09:52
  • 2
    Hibernate Criteria **requires** mapping your tables to persistent classes and defining associations. – Vlad Nov 17 '11 at 09:56