jpa native query not returning all values - java

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.

Related

hql query to select values within some date

i came into a situation where i want to select rows which meets within some date.
i am using oracle and my query looks like :-
Query
SELECT d.id,d.ADVERTISEMENTCODE,d.LASTDATEOFFORMSUBMISSION,e.LASTDATEFORFORMSUBMISSION,
d.studentMasterId FROM CANDIDATEAPPEARAGAINSTADVTCODE d LEFT OUTER JOIN ADVERTISEMENTCODE e
ON d.ADVERTISEMENTCODE = e.ADVERTISEMENTCODE WHERE paymentStatus='Pending'
and studentMasterId='8670' and
TO_DATE(e.LASTDATEFORFORMSUBMISSION,'dd/mm/yyyy')+4 > TO_DATE('11/05/2017','dd/mm/yyyy');
this is a running query and it returns exactly what i want.
but now i want to do it with hibernate query language. how can it be done?

OrientDB use result of subquery to search index

I am using OrientDb 2.1-rc4 as a document database. I have a MyClass class that gets updated very frequently by a multithreded application. To improve performance I removed the link from the State class to MyClass, and added a link and index from MyClass to State. In order to get all instances of MyClass I need to get a list of State rids and then query the index. This query gets my State rids.
SELECT DISTINCT(roles.views.states.#rid)
FROM #12:1
returns
[["#14:0","#14:1"]]
This query finds the correct rids in the index.
SELECT FROM index:MyClass.state
where key in [#14:0,#14:1]
When I put the two queries together.
SELECT FROM index:MWorkUnit.state
where key in (SELECT DISTINCT(roles.views.states.#rid) FROM #12:1)
I get the following error.
java.lang.ClassCastException: com.orientechnologies.orient.core.sql.query.OSQLSynchQuery cannot be cast to java.util.List
How do I get OrientDB to treat my subquery as an rid list?
Edit:
This query works when not using the index.
SELECT FROM MWorkUnit
where state IN (SELECT expand(roles.views.states).#rid FROM #12:1)
This seems a bug on query against indexes: sub queries are not correctly evaluated. Please could you open a new issue or that?
This workaround should work:
SELECT FROM index:MWorkUnit.state
LET $list = (SELECT DISTINCT(roles.views.states.#rid) FROM #12:1)
where key in $list

Spring data JPA - The encapsulated expression is not a valid expressio

While running the below query to get the latest message with the latest data using the createdOn column
i get the below error
select count(m) from MessageWorkFlowStatus mwfs1 where mwfs1.createdOn =(select max(createdOn) from MessageWorkFlowStatus mwfs2 where mwfs1.status= 'NEW' or mwfs1.status='IN PROGRESS')
The encapsulated expression is not a valid expression
Please let me know if i can run Query this way
First of all, count(m) is not correct. It should either be count(*) or count(mwfs1). Secondly, in your inner query, you are using status column from the outer query table (mswfs1) which is logically wrong. It should instead be mwfs2.status = 'NEW' or mwfs2.status = 'IN PROGRESS'.
I think your query should be:
select count(mwfs1)
from MessageWorkFlowStatus mwfs1
where mwfs1.createdOn = (
select max(createdOn)
from MessageWorkFlowStatus mwfs2
where mwfs2.status= 'NEW' or mwfs2.status='IN PROGRESS')

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.

How to use sql query with IN for a select statement in Java

I need to us a sql query in java that has an IN clause in it. I have seen suggestions for doing this when the IN clause is a predetermined set, such as numbers which won't work in this case because the set will be dynamic. Here is what I am trying to do:
select c.cust_name
from cust_acct c, has h
where c.cust_id=h.cust_id
and h.sh_add in (select a.sh_add from address a where sh_st='VA')
I do not get any faults. That is, the code runs I just get no results for this particular query
Have you checked that you are getting the right intermediate results from select a.sh_add from address a where sh_st='VA'?
You could just rewrite the query as:
select c.cust_name
from cust_acct c
inner join has h
on c.cust_id=h.cust_id
inner join address a
on h.sh_add = a.sh_add
where a.sh_st = 'VA'

Categories

Resources