matthew016
asked on
[Hibernate] HQL Query not working
Hi ,
My HQL Query is not working,
any help ?
Thank you,
query =
"SELECT COUNT(*) FROM " +
"(" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdOutward+"' " +
"AND status LIKE 'Active' " +
"AND userId IN (" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdReturn+"' " +
"AND status = 'Active' " +
")" +
")";
results = session.find(query);
[25/07/07 11:11:27:880 CEST] 5f1d4c26 SystemErr R net.sf.hibernate.QueryExce ption: in expected: SELECT [SELECT COUNT(*) FROM (SELECT USERID FROM TB_ATB_BOOKING WHERE TRIPID='449' AND STATUS = 'Active' AND USERID IN (SELECT USERID FROM TB_ATB_BOOKING WHERE TRIPID = '451' AND STATUS = 'Active' ))]
at net.sf.hibernate.hql.FromP arser.toke n(FromPars er.java:10 2)
at net.sf.hibernate.hql.Claus eParser.to ken(Clause Parser.jav a:87)
at net.sf.hibernate.hql.Prepr ocessingPa rser.token (Preproces singParser .java:123)
at net.sf.hibernate.hql.Parse rHelper.pa rse(Parser Helper.jav a:29)
at net.sf.hibernate.hql.Query Translator .compile(Q ueryTransl ator.java: 149)
at net.sf.hibernate.hql.Query Translator .compile(Q ueryTransl ator.java: 138)
at net.sf.hibernate.impl.Sess ionFactory Impl.getQu ery(Sessio nFactoryIm pl.java:29 3)
at net.sf.hibernate.impl.Sess ionImpl.ge tQueries(S essionImpl .java:1554 )
at net.sf.hibernate.impl.Sess ionImpl.fi nd(Session Impl.java: 1525)
at net.sf.hibernate.impl.Sess ionImpl.fi nd(Session Impl.java: 1513)
at net.sf.hibernate.impl.Sess ionImpl.fi nd(Session Impl.java: 1505)
[...]
My HQL Query is not working,
any help ?
Thank you,
query =
"SELECT COUNT(*) FROM " +
"(" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdOutward+"' " +
"AND status LIKE 'Active' " +
"AND userId IN (" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdReturn+"' " +
"AND status = 'Active' " +
")" +
")";
results = session.find(query);
[25/07/07 11:11:27:880 CEST] 5f1d4c26 SystemErr R net.sf.hibernate.QueryExce
at net.sf.hibernate.hql.FromP
at net.sf.hibernate.hql.Claus
at net.sf.hibernate.hql.Prepr
at net.sf.hibernate.hql.Parse
at net.sf.hibernate.hql.Query
at net.sf.hibernate.hql.Query
at net.sf.hibernate.impl.Sess
at net.sf.hibernate.impl.Sess
at net.sf.hibernate.impl.Sess
at net.sf.hibernate.impl.Sess
at net.sf.hibernate.impl.Sess
[...]
looks more like sql than hql :)
You're mixing HQL and native SQL here. The query you want to execute is a native query and should be executed using:
session.createSQLQuery(que ry).list() ;
to get this in HQL you should start from
select count(distinct booking.userID) from Booking booking where ...
session.createSQLQuery(que
to get this in HQL you should start from
select count(distinct booking.userID) from Booking booking where ...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
I have always used results = session.find(query); and it works for all the other methods.
I chanegd to this :
query =
"SELECT COUNT(distinct bookId) FROM " +
"(" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdOutward+"' " +
"AND status LIKE 'Active' " +
"AND userId IN (" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdReturn+"' " +
"AND status LIKE 'Active' " +
")" +
")";
results = session.find(query);
But I have the same error
I have always used results = session.find(query); and it works for all the other methods.
I chanegd to this :
query =
"SELECT COUNT(distinct bookId) FROM " +
"(" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdOutward+"' " +
"AND status LIKE 'Active' " +
"AND userId IN (" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdReturn+"' " +
"AND status LIKE 'Active' " +
")" +
")";
results = session.find(query);
But I have the same error
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The error has changed now
[25/07/07 12:56:20:250 CEST] 78f4cc26 SystemErr R net.sf.hibernate.QueryExce ption: undefined alias: userId [SELECT count(userId) FROM com.fortis.atb.bl.dto.Book ing WHERE tripId = '449' AND status LIKE 'Active' AND userId IN (SELECT userId FROM com.fortis.atb.bl.dto.Book ing WHERE tripId = '451' AND status LIKE 'Active' )]
at net.sf.hibernate.hql.PathE xpressionP arser.toke n(PathExpr essionPars er.java:10 3)
[25/07/07 12:56:20:250 CEST] 78f4cc26 SystemErr R net.sf.hibernate.QueryExce
at net.sf.hibernate.hql.PathE
what do you mappings look like?
ASKER
I have put some alisas like Booking b,
now it seems to work,
Let me check
now it seems to work,
Let me check
Another shot (need to use shotgun tactics without seeing some code):
"SELECT count(b) " +
"FROM Booking b " +
"WHERE tripId = '"+tripIdOutward+"' " +
"AND status LIKE 'Active' " +
"AND userId IN (" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdReturn+"' " +
"AND status LIKE 'Active' " +
")";
"SELECT count(b) " +
"FROM Booking b " +
"WHERE tripId = '"+tripIdOutward+"' " +
"AND status LIKE 'Active' " +
"AND userId IN (" +
"SELECT userId " +
"FROM Booking " +
"WHERE tripId = '"+tripIdReturn+"' " +
"AND status LIKE 'Active' " +
")";
count(*) should be fine
ASKER
YESSSSSSSS by removing the upper select like u posted it works ! (just had to add the aliases)
I will close this question,
Could you just tell me, if you know, why it works like this and now with the useless select (because I tested it in oracle and they both works in oracle)
I will close this question,
Could you just tell me, if you know, why it works like this and now with the useless select (because I tested it in oracle and they both works in oracle)
ASKER
*now = not
> Could you just tell me, if you know, why it works like this and now with the
> useless select (because I tested it in oracle and they both works in oracle)
because its valid in oracle sql, but not in hql.
They are not the same thing.
> useless select (because I tested it in oracle and they both works in oracle)
because its valid in oracle sql, but not in hql.
They are not the same thing.
As objects mentioned: HQL is not Oracle SQL.
Hibernate will use the HQL (which is more object oriented) and convert it using the configured dialect and the mappings to SQL (table oriented).
Hibernate will use the HQL (which is more object oriented) and convert it using the configured dialect and the mappings to SQL (table oriented).