Is a IN subquery matching multiple columns possible in JPQL? - java

I have a SQL query I'm trying to convert to JPQL. The query is as follows :
SELECT *
FROM MyTable
WHERE (myFirstColumn, mySecondColumn) IN (
SELECT myFirstColumn, max(mySecondColumn)
FROM MyTable
GROUP BY myFirstColumn
)
My conversion attempt is straightforward :
select myObject
from MyObject as myObject
where (myObject.myFirstValue, myObject.mySecondValue) in (
select subMyObject.myFirstValue, max(subMyOject.mySecondValue)
from MyObject as subMyObject
group by subMyObject.myFirstValue
)
MyObject is mapped to MyTable (using annotations).
If I understand the JPQL docs on the IN statement (http://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/jpa_langref.html#jpa_langref_in), and I'm really not sure I do, such a direct conversion isn't possible. Is there another way ?

Perhaps you can change the query a little. You can use EXISTS instead of IN.
select myObject
from MyObject myObject
where exists
(
select subMyObject.myFirstValue, max(subMyOject.mySecondValue)
from MyObject subMyObject
where myObject.myFirstValue = subMyObject.myFirstValue
group by subMyObject.myFirstValue
having max(subMyOject.mySecondValue) = myObject.mySecondValue
)

In the end, I couldn't find a way and had Java do the heavy lifting (ie. sorting one the second value and finding the biggest). It's not as pretty but it works.

Related

Equivalent UTL_RAW.BIT_OR in JPQL

I'm trying to make a namedquery with a result DTO where there is a field that I have to calculate using that function. Is there any equivalent?
SELECT new BitsDTO(
UTL_RAW.BIT_OR(b.bitsA, b.bitsB) as bitsCalculated)
FROM Bits b
where b.id = ?;
Thanks.
If you want to call a database function in JPQL you can do it like this
SELECT new BitsDTO(
function("UTL_RAW.BIT_OR", b.bitsA, b.bitsB)
)
FROM Bits b
where b.id = ?
That way you can call any database function. The first argument is the function name.
#Simon Martinelli was almost right. Calling database functions from a top-level select clause item requires casting so that Hibernate knows how to read the value from JDBC like this: select new BitsDTO(cast(function('UTL_RAW.BIT_OR', b.bitsA, b.bitsB) as java.lang.String) ...
The alternative to this is that you register the function as SQLFunction in your dialect and provide a proper return type for it. Then you can use is like you had in your original question.

jpa native query not returning all values

I am using jpa native query , but its not returning values from salias it returns values from S
Query query = em.createNativeQuery("Select S.\"MESSAGE\",S.\"DESTINATION\",S.\"SENT_DATE\",S.\"CLIENT_TRACKING_ID\",S.\"MESSAGE_COST\",S.\"sTId\",salias.\"STATUS\",salias.timeDate from \"sent_sms_view\" S left join ( Select Distinct on (\"SMS_ID\") R.\"SMS_ID\",R.\"STATUS\",R.timeDate from \"sms_receipt_view\" R Order By R.\"SMS_ID\",R.timeDate Desc)As salias on S.\"SYSTEM_TRACKING_ID\"=salias.\"SMS_ID\" where S.Id_systemUser=:systemUser and S.\"CLIENT_TRACKING_ID\"=:cTId");
query.setParameter("cTId", cTId);
query.setParameter("systemUser", systemUser);
if (query.getResultList().size() > 0){
List<Object> resultat = query.getResultList();
This is the Postgres query and it works fine
Select S."MESSAGE",S."DESTINATION",S."SENT_DATE",S."CLIENT_TRACKING_ID",S."MESSAGE_COST",S."sTId" ,salias."STATUS",salias.timeDate
from "sent_sms_view" S
left join ( Select Distinct on ("SMS_ID") R."SMS_ID",R."STATUS",R.timeDate from "sms_receipt_view" R Order By R."SMS_ID",R.timeDate Desc)As salias
on S."SYSTEM_TRACKING_ID"=salias."SMS_ID"
where S.Id_systemUser='101' and S."CLIENT_TRACKING_ID" ='abda';
Can anyone tell me what i am doing wrong.
I'm only guessing what you might be trying to do, since you haven't told us, but here's how I'm guessing it should probably look like:
SELECT S."MESSAGE", S."DESTINATION", S."SENT_DATE", S."CLIENT_TRACKING_ID", S."MESSAGE_COST", S."sTId", salias."STATUS", salias.timeDate
FROM "sent_sms_view" S
INNER JOIN "sms_receipt_view" AS salias on (S."SYSTEM_TRACKING_ID" = salias."SMS_ID")
WHERE S.Id_systemUser=:systemUser AND S."CLIENT_TRACKING_ID"=:cTId
However I don't see why you would have numerical IDs, such as Id_systemUser stored as strings. In fact that variable name indicates horrible database design. CamelCasing combined with underscores is something you must categorically avoid.
And you must never call query.getResultList() twice if you're looking for the same results. Simply store the List to a local variable and then use it.

jooq - execute string as subquery

I have a query, represented by a string:
final String q = "select 1 union select 2 union select 3";
This string comes from an external source (configuration), hence it is a string. In the real scenario, the query is ofcourse more meaningful.
I would like to execute this query as a subquery within a jOOQ type-safe query. The following works, but it is not really what I want:
System.out.println(<context>.select().from(DSL.table("person")).where(DSL.field("identifier").in(
<context>.fetch(q).intoArray(0)
)).fetch());
The problem here is that I am essentially executing two queries. This introduces overhead.
Is it possible to execute the string-query as a real subquery? I somehow have to convert the string-query to a Select<Record1> instance (I guess), but I cannot find how to do that.
There are a variety of places where you can inject a Select type as plain SQL. For instance:
As a plain SQL WHERE clause:
<context>.select()
.from(DSL.table("person"))
.where(
"identifier in ({0})", DSL.resultQuery(q)
)
.fetch();
As a plain SQL Table:
<context>.select()
.from(DSL.table("person"))
.where(DSL.field("identifier").in(
DSL.select().from("(" + q + ")")
))
.fetch();
There are others. The important thing to notice is that by using plain SQL, you have the possibility to embed your own SQL strings in templates that have enumerated placeholders
... {0} ... {1} ...

Using two fields with "in" operator in QueryDSL

I have to write this query using QueryDSL:
select *
from table
where(field1, field2) in (
select inner_field_1, inner_field2
from ...
);
However, I don't know how to use two fields (field1 and field2) with an "in" operator in QueryDSL. I have been looking for it in the documentation but I haven't seen any example of two fields.
This is what I have so far:
Expression<?>[] projection = {
table.field1,
table.field2
};
SQLSubQuery outterQuery= new SQLSubQuery()
.from(table)
.where([some expression].in(inneryQuery.list(projection))) // ???
.groupBy(contentcache1.programId, contentcache1.id);
Any help would be appreciated
Thank you very much in advance
You can express it via
SQLSubQuery outerQuery = new SQLSubQuery()
.from(table)
.where(Expressions.list(column1, column2, ...).in(inneryQuery.list(projection)))
.groupBy(contentcache1.programId, contentcache1.id);
You can rewrite your original query as:
select *
from table, (select distinct inner_field_1, inner_field2 from ...) subquery
where field1 = subquery.field1 and field2 = subquery.field2
Then you don't have to use the IN operator.
You can manually transform your row-value-expression IN predicate into an equivalent EXISTS predicate, which should probably work with QueryDSL. Some details are explained in this blog post, which essentially explains how jOOQ automatically handles such SQL transformations for you, operating directly on the SQL AST, you'd write:
DSL.using(configuration)
.select()
.from(TABLE)
.where(row(TABLE.FIELD1, TABLE.FIELD2).in(
select(INNER_FIELD1, INNER_FIELD_2)
.from(...)
))
Your original query:
select *
from table
where(field1, field2) in (
select inner_field_1, inner_field_2
from ...
);
Is equivalent to this one:
select *
from table
where exists (
select 1
from ...
where table.field1 = inner_field_1 and table.field2 = inner_field2
)
... which I'm sure you can express with QueryDSL (unfortunately, I don't know the API well enough to show the actual query).
Note on compatibility
Chances are that your database doesn't support this kind of row value expression predicate anyway, in case of which you're on the safe side with EXISTS. At least these databases do support that predicate:
DB2
HSQLDB
MySQL
Oracle
Postgres

Convert Postgresql query to Hibernate

In my Java Web application I use Postgresql and some data tables are filled automatically in server. In the database I have a STATUS table like below:
I want to select the data related to a vehicle between selected dates and where the vehicle stayed connected. Simply I want to select the data which are green in the above table which means I exactly want the data when firstly io1=true and the data when io1=false after the last io1=true. I have postgresql query statement which exactly gives me the desired data; however, I have to convert it to HQL because of my application logic.
working postgresql query:
WITH cte AS
( SELECT iostatusid, mtstrackid, io1,io2,io3, gpsdate,
(io1 <> LAG(io1) OVER (PARTITION BY mtstrackid
ORDER BY gpsdate)
) AS status_changed
FROM iostatus
WHERE mtstrackid = 'redcar' AND gpsdate between '2014-02-28 00:00:00' and '2014-02-28 23:59:59'
)
SELECT iostatusId, mtstrackid, io1, io2, io3,gpsdate
FROM cte
WHERE status_changed
OR io1 AND status_changed IS NULL
ORDER BY gpsdate ;
How should I convert the above query to HQL or how could I retrieve the desired data with HQL?
The goal of hibernate is mapping database entities to java objects. This kind of complex queries are not entities themselves. This is against the spirit of hibernate.
If this query generates an entity in your application logic, I recommend putting the results into a table and applying Hibernate queries to that table.
If this query generates some kind of aggregation or summary, there are two possible ways:
One way is you compute this aggregation/summary in your application after retrieving entities from iostatus table with hibernate.
If this query has nothing to do with your application logic then you can use Native SQL interface of Hibernate and execute the query directly. (You can even use JPA if you are willing to manipulate two database connections.)
If you absolutely need to convert it to HQL, you need to eliminate the partition function. If the order of iostatusId is identical to the order of gpsdate, you can do it similar to
SELECT i2.*
FROM iostatus i1
INNER JOIN iostatus i2 ON i1.iostatusId = i2.iostatusId - 1
AND i1.io1 <> i2.io1
AND i1.mstrackid = i2.mstrackid
WHERE i2.mtstrackid = 'redcar' AND
i2.gpsdate between '2014-02-28 00:00:00' and '2014-02-28 23:59:59'
If gpsdate is no way related to iostatusId then you need something like
SELECT i2.*
FROM iostatus i1
INNER JOIN iostatus i2 ON i1.gpsdate < i2.gpsdate
AND i1.io1 <> i2.io1
AND i1.mstrackid = i2.mstrackid
WHERE i2.mtstrackid = 'redcar' AND
i2.gpsdate between '2014-02-28 00:00:00' and '2014-02-28 23:59:59' AND
NOT EXISTS (SELECT * FROM iostatus i3
WHERE i3.gpsdate > i1.gpsdate AND
i2.gpsdate > i3.gpsdate AND
i3.io1 = i1.io1 AND
i1.mstrackid = i3.mstrackid)
I guess both of the queries can be converted to HQL, but I'm not positively sure.
By the way I must warn you that, these methods might not perform better then finding the changes in your application, because they involve joining the table onto itself, which is an expensive operation; and the second query involves a nested query after the join, which is also quite expensive.

Categories

Resources