I am trying to do a simple inner join using GORM's executeQuery but getting a QuerySyntaxException.....I believe my hql is ok. Here is my query
def query = Institution.executeQuery("select longName from Institution inner join TacticalIndustryCode.idInstitution")
log.info(query.size())
I tried this with same error too:
def query = Institution.executeQuery("from Institution inner join TacticalIndustryCode.id")
Here is my exception that I am receiving
org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'null.idInstitution' [select longName from erebus.industryGroup.Institution inner join TacticalIndustryCode.idInstitution]
at erebus.industryGroup.TacticalIndustryCodeController$$ENunaZiV.list(TacticalIndustryCodeController.groovy:20)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Make sure you have a field called 'idInstitution' in the domain 'TacticalIndustryCode'.
Remember that when you write HQLs, you don't refer to the table or its columns. Instead you use the mapped class and their properties.
def query = Institution.executeQuery("select inst.longName from Institution as inst inner join inst.tacticalIndustryCode")
For more info follow the link:
http://grails.asia/grails-hql-join-examples
Related
I am using the below as a JpaRepository interface method query
#Query("SELECT DISTINCT order.status, COUNT(*) FROM OrderEntity order WHERE order.customerNumber = ?1 GROUP BY order.status")
During app startup I am getting the following exception -
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: order near line 1, column 17 [SELECT DISTINCT order.status AS status, COUNT(*) AS count FROM debomitra.cmw.orders.entity.OrderEntity order WHERE order.customerNumber = ?1 GROUP BY order.status]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:297)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:155)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:600)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:709)
... 116 more
Is there any other way to write DISTINCT/COUNT query in JPA or am I doing something wrong?
Please provide me with some pointers.
I think the query will work if you modify it like below-
SELECT DISTINCT OrderEntity.status, COUNT(*) FROM OrderEntity WHERE OrderEntity.customerNumber = ?1 GROUP BY OrderEntity.status
The issue is with the order keyword in the JPQL. It is expecting a "by" keyword after the "order".
Whoever is reading this try avoiding "order" keyword in a statement expect at the end where you really need to issue a ORDER BY statement.
I have written the following hive query. Here I am trying to use a column (msg) of Map data type in my join clause.
select p.p_id, count(*) from prod_json n
inner join res_pan p on n.msg["mid"] = p.id
where n.cat='XYX'
group by p.p_id limit 10;
This query always fails with error message
[Error getting row data with exception java.lang.ClassCastException:
java.lang.String cannot be cast to org.openx.data.jsonserde.json.JSONObject at
org.openx.data.jsonserde.objectinspector.JsonMapObjectInspector.getMap(Json
MapObjectInspector.java:40) at
org.apache.hadoop.hive.serde2.SerDeUtils.buildJSONString(SerDeUtils.java:317) at
org.apache.hadoop.hive.serde2.SerDeUtils.buildJSONString(SerDeUtils.java:353) at
org.apache.hadoop.hive.serde2.SerDeUtils.getJSONString(SerDeUtils.java:197) at
org.apache.hadoop.hive.serde2.SerDeUtils.getJSONString(SerDeUtils.java:183) at
org.apache.hadoop.hive.ql.exec.MapOperator.toErrorMessage(MapOperator.java:
529) at
org.apache.hadoop.hive.ql.exec.MapOperator.process(MapOperator.java:502) at
org.apache.hadoop.hive.ql.exec.mr.ExecMapper.map(ExecMapper.java:170) at
org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:54) at
org.apache.hadoop.mapred.MapTask.runOldMapper(MapTask.java:453) at
I think I was able to solve the problem. I re-wrote my query as
select t2.p_id, count(*)
from (select cat, msg["mid"] as mid from prod_json) t1
join (select id, p_id from res_pan) t2
on t1.mid = t2.id
where t1.cat = 'XYZ'
group by t2.p_id
So basically don't try to use the map column directly but first platten the map into columns by using inner queries and then join on those.
How do i write below query in Grails either using criteria query or executeQuery?
select * from table1 as t1 left Join(select * from table2 where id=2)as t2 On t2.table1=t1.id ;
Unlike SQL, in which you query tables, GORM/Hibernate queries query domain classes. Yes, it boils down to tables, but from the Criteria, WHERE, and HQL point-of-view, it's domain classes. Which is why, as Koloritnij pointed out, knowledge of the domain model is necessary for writing the query.
SQL vs GORM
One difference in how SQL and GORM perform joins is that SQL joins are created on-the-fly, in the SQL itself. Whereas GORM joins are predetermined by the domain class associations. This means that in HQL you cannot join to a sub-query. You can read more about such differences here.
An example
That being said, using your example SQL I made an assumption about your domain model:
class DomainA {
}
class DomainB {
DomainA a
}
In the domain model above, DomainB (table2) has a uni-directional many-to-one association with DomainA (table1). An HQL similar to your SQL is as follows:
def hql = 'SELECT a, b FROM DomainB AS b RIGHT OUTER JOIN b.a AS a WHERE b.id = :id'
The HQL can be executed like this:
def result = DomainB.executeQuery(hql, [id: 2])
Here you go (if you have Domain Classes with foreign key):
def query
query = sessionFactory.getCurrentSession().createCriteria(table2.class)
query = query.createAlias("table1", "table1Alias", CriteriaSpecification.LEFT_JOIN, Restrictions.in( 'table1Alias.id', 'table2.id'))
Restrictions.eq( "id", 2)
If not, you have to use raw SQL:
def dataSource
Sql sql = new Sql(dataSource)
sql.exequteQuery("""....""")
This is my HQL query, but it isn't working and is throwing an error.
Hql query:
SELECT
*
FROM
TABLEA A
LEFT JOIN
A.TABLEB B
WHERE
A.COLUMNNAME = B.COLUMNAME
and it causes this error:
org.hibernate.QueryException:
This Query caught Exception. could not resolve property: of TABLEB:TABLEA.
How can I solve this problem? Actually I retrieved a value from more than one table. This query doesn't work with CreateQuery(strQuery).
In HQL you can use LEFT JOIN only with linked property in main entity:
Sample
EntityA has an object entityB of type EntityB so you can
SELECT A FROM EntityA A LEFT JOIN A.entityB B WHERE ...
IF EntityA haven't entityB property but is EntityB have a property entityA, you can't write this:
SELECT A FROM EntityA LEFT JOIN EntityB B WHERE B.entityA = A
because you have an error. This is an Hibernate issue not resolved yet.
i have 3 tables User, TeamMember and Assessment all three have the user_id column, User is linked to TeamMember by userid and TeamMember and Assessment are linked with user_id(TeamMember) & rater,rated(Assessment)
Im trying to join in with this
from a in db.Assessments
join u in db.Team_Members on a.rated equals u.user_id
join u2 in db.Team_Members on a.rater equals u2.user_id
It's throwing this error
The type of one of the expressions in the join clause is incorrect.
Type inference failed in the call to 'Join'.
Any idea what going on?
You need to make sure a.rated / u.user_id and a.rater / u2.user_id are the same type