Is there a way to do select distinct trim(addressInfo.city) in JPA ?
I got this error (without the trim it works fine)
Caused By: java.sql.SQLSyntaxErrorException: ORA-01791: not a SELECTed expression
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:879)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:450)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
Truncated. see log file for complete stacktrace
>
and Here the generated SQL:
select distinct trim(addressinf3_.city) as col_0_0_
from t_application applicatio0_
left outer join t_user user1_ on applicatio0_.first_signatory=user1_.id
left outer join t_company company2_ on user1_.company=company2_.id
left outer join t_address_info addressinf3_ on company2_.address=addressinf3_.id
where applicatio0_.discriminator='SERVICE' and (addressinf3_.city is not null) order by addressinf3_.city
This exception usually happens when you have some column/expression in order by clause which you don't have in select clause, in combination with distinct. Without knowing the actual JPQL query you are executing, here is an example that should work
select distinct trim(addressInfo.city)
from AddressInfo addressInfo
where <some conditions>
order by trim(addressInfo.city)
If you use trim(addressInfo.city) in select, then you must use it also in order by. Without trim it won't work.
Related
I have the following Hibernate HQL query:
select t from Term t join ApprovedCourse ap on t.id = ap.term.id group by t order by t desc
It's failing with the
ORA-00979: not a GROUP BY expression
error because Oracle insists that all select values be in the group by. Hibernate, of course, is hiding the various fields of the Term object from us, letting us deal with it as a Term and not Term.id. (This query works on Postgres, by the way. Postgres is more liberal about its group by requirements.)
Hibernate is producing the following SQL:
select term0_.id as id1_12_, term0_.semester_id as semester_id2_12_, term0_.year_id as year_id3_12_
from term term0_
inner join approved_course approvedco1_
on (term0_.id=approvedco1_.term_id)
group by term0_.id
order by term0_.id desc
I've tried just removing the select t from the start of the query, but then Hibernate assumes that I'm selecting both the Term and ApprovedCourse objects, and that makes things worse.
So how do I make this work in a Hibernate way?
I found that I could get what I want by replacing the group by clause with a distinct in the select clause. Here's the resulting query:
select distinct(t) from Term t join ApprovedCourse ap on t.id = ap.term.id order by t desc
This is my query, if i run the error is : Error in query: ORA-00907: missing right parenthesis, anybody can solve my problem?
Create table r_tcash_loci_act_tmp AS (
SELECT DISTINCT
R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI,
R_TCASH_ACT_MSISDN.MSISDN AS MSISDN_ACT,
R_TCASH_LOCI_ACT.AREA,
R_TCASH_LOCI_ACT.REGIONAL,
R_TCASH_LOCI_ACT.BRANCH,
R_TCASH_LOCI_ACT.SUB_BRANCH,
R_TCASH_LOCI_ACT.CLUSTERX,
R_TCASH_LOCI_ACT.UPDATED,
R_TCASH_ACT_MSISDN.DAILY,
R_TCASH_ACT_MSISDN.TOTAL_TRX,
R_TCASH_ACT_MSISDN.TOTAL_VOL
FROM R_TCASH_ACT_MSISDN
INNER JOIN R_TCASH_LOCI_ACT
ON R_TCASH_LOCI_ACT.MSISDN = R_TCASH_ACT_MSISDN.MSISDN
GROUP BY R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI
HAVING COUNT(R_TCASH_LOCI_ACT.MSISDN ) > 10);
It's a simple syntax error. You included the column alias in the GROUP BYclause (probably a cut'n'paste error).
GROUP BY R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI
So just remove the AS MSISDN_LOCI.
Although given that you have a DISTINCT clause and no aggregated columns it's a mystery why you have the GROUP BY at all. You should remove the whole line.
I have the following Oracle SQL query I want to execute as a NativeQuery in JPA with the Play Framework, but for some reason, I always get an ORA-00907: missing right parenthesis error.
SELECT
a.EMPLOYEE_ID,
a.FIRST_NAME,
a.LAST_NAME,
a.EMAIL,
a.PHONE_NUMBER,
TO_CHAR(a.HIRE_DATE, 'YYYY/MM/DD') HIRE_DATE,
a.JOB_ID,
a.SALARY,
a.COMMISSION_PCT,
a.MANAGER_ID,
a.DEPARTMENT_ID,
CONCAT(b.FIRST_NAME, CONCAT(' ', b.LAST_NAME)) MANAGER_NAME,
JOBS.JOB_TITLE,
DEPARTMENTS.DEPARTMENT_NAME
FROM
EMPLOYEES a
LEFT JOIN
JOBS
ON
a.JOB_ID = JOBS.JOB_ID
LEFT JOIN
EMPLOYEES b
ON
a.MANAGER_ID = b.EMPLOYEE_ID
LEFT JOIN
DEPARTMENTS
ON
a.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID
ORDER BY
a.EMPLOYEE_ID
ASC
I tried testing this query in the Oracle SQL Developer and it works, so I don't know if JPA automatically inserts an open left parenthesis in the query. The Play console doesn't really help much since it doesn't point out exactly where the ORA-00907 error is trapped in.
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.
I have a problem with a hql query, I use the last version on hibernate on jboss7.
So, I have table1 linked to table2 and table2 has an attribute (SET) references to table3.
Here is a small schema
TABLE1-----TABLE2(attribute: transfer manyToMany) TABLE3(attribute elements)
here is my query:
select t1 from TABLE1 t1
left join fetch t1.TABLE2 t2
left join fetch t2.transfers tf (here we have a list of T3)
How to load the elements in T3 with a left join fetch, if it's possible at all?
I try tf.elements but I receive indexOutOfBounds exception.
I tried to add the T3 in the select statement and to add a condition but I didn't find a condition on the list tf.
Thanks