#query with named #param returning empty result set when using sql IN - sql

Btw this is already working with a jdbc template based solution that I wrote. It's not me wanting to do the rewrite, it is the tec lead's idea. It is giving me a headache.
Lets not get into the discussion on why this is or isn't a good idea, but rather how to make it work.
in my repo
#Repository
interface Foo...extends JpaRepository
this works:
#Query(value = "SELECT t.td, em.ct, j.dsc AS bs, ct.dsc AS ct_name, "
+ "t.tn, et.dsc, f.f_id, f.f_life, f.fl_name, em.f_note "
+ "FROM ext_master em join tax t on em.td = t.td "
+ "join bs j on j.bs = t.bs "
+ "join ex_type et on em.ex_type = et.ex_type "
+ "join ct ct on ct.ct = em.ct "
+ "left join ex_f ef on em.td = ef.td and em.ct = ef.ct "
+ "left join f f on ef.f_id = f.f_id "
+ "WHERE em.ct in ( 'CC' ) "
+ "AND t.state IN ('FF','AK','AL','GA')"
+ "ORDER BY bs, t.tn", nativeQuery = true)
List<Object> getExtensionsByCustTypeAndState(#Param("custTypes") String custTypes, #Param("states") String states);
this doesn't returns empty result set
#Query(value = "SELECT t.td, em.ct, j.dsc AS bs, ct.dsc AS ct_name, "
+ "t.tn, et.dsc, f.f_id, f.f_life, f.fl_name, em.f_note "
+ "FROM extension_master em join tax t on em.td = t.td "
+ "join bs j on j.bs = t.bs "
+ "join exemption_type et on em.ex_type = et.ex_type "
+ "join ct ct on ct.ct = em.ct "
+ "left join ex_f ef on em.td = ef.td and em.ct = ef.ct "
+ "left join f f on ef.f_id = f.f_id "
+ "WHERE em.ct in ( :custTypes ) "
+ "AND t.state IN ('FF', :states)"
+ "ORDER BY bs, t.tn", nativeQuery = true)
List<Object> getExByCustTypeAndState(#Param("custTypes") String custTypes, #Param("states") String states);
both can have one or multiple values, I tried:
with both params like {:states} :#{#states}
with the value inside the param
surrounded by " or by '
passing the whole line in the parameter like "AND t.state IN ('FF','AK','AL','GA')"
passing the 'FF','AK','AL','GA' in the states param and subtituting the in the query with
+ "AND t.state IN ( :states)"
I did write a simple query to make sure I am getting the parameters passed in right. so that's working.
Can anyone write this right, so that the in statements work, or tell me why it is not possible.
Thank you.

You pass all the possible values in one parameter that's why you don't get the results you are expecting.
You have to change the where clause like this:
"WHERE em.ct in ( :custTypes1, :custTypes2, :custTypes3 .... :custTypesN) "

this ended up being the simplest solution
instead of :
"WHERE em.ct in ( :custTypes ) "
+ "AND t.state IN ('FF', :states)"
do:
"WHERE em.ct in :custTypes "
+ "AND t.state IN :states "
and add the FF to the states string on before I call the function.

Related

"ORA-00920: invalid relational operator" Error

select
ic.item_name,
lh.locn_brcd from_locn,
lh2.locn_brcd to_locn,
wl.from_container,
wl.to_container,
wl.units,
wl.prev_from_container_status prev_from_lpn_status,
wl.curr_from_container_status curr_from_lpn_status,
wl.prev_to_container_status prev_to_lpn_status,
wl.curr_to_container_status curr_to_lpn_status,
wl.work_batch_number,
wl.transaction_name,
wl.action,
wl.work_id,
wl.date_updated,
wl.source_updated,
wl.tote_number,
wl.chute
from m_work_log wl
LEFT join item_cbo ic on wl.item_id=ic.item_id
left join locn_hdr lh on wl.from_location_id = lh.locn_id
left join locn_hdr lh2 on wl.to_location_id = lh2.locn_id
where wl.action in (:action)
and trunc(wl.date_updated) between :start_date and :end_date
and (ic.item_name in (:list) OR
wl.source_updated = :username OR
wl.to_container in (:LPNList) OR
(:list is null and :username is null and :LPNList is null)
)
order by date_updated desc
Hi everyone,
when I run this code through Oracle SQL Developer and I add two items to the :list parameter and two items to the :action parameter it works fine. But when I run this through SSRS (report builder ) it fails to run and I get an "ORA-00920: invalid relational operator". I'm new to SQL and i'm not sure what I am doing incorrectly here. Any help is greatly appreciated. Thanks!
There are 2 ways to do this:
Multiple Value Parameter:
First of all, you must use Oracle provider, multiple value paramaters does not work with neither ODBC nor OLEDB connections (reference).
Here is an external link explaining in detail here.
Using an expression as the query by putting the whole thing like this ="query_here"
="select "
+ " ic.item_name,"
+ " lh.locn_brcd from_locn,"
+ " lh2.locn_brcd to_locn,"
+ " wl.from_container,"
+ " wl.to_container,"
+ " wl.units,"
+ " wl.prev_from_container_status prev_from_lpn_status,"
+ " wl.curr_from_container_status curr_from_lpn_status,"
+ " wl.prev_to_container_status prev_to_lpn_status,"
+ " wl.curr_to_container_status curr_to_lpn_status,"
+ " wl.work_batch_number,"
+ " wl.transaction_name,"
+ " wl.action,"
+ " wl.work_id,"
+ " wl.date_updated,"
+ " wl.source_updated,"
+ " wl.tote_number,"
+ " wl.chute"
+ "from m_work_log wl"
+ " LEFT join item_cbo ic on wl.item_id=ic.item_id"
+ " left join locn_hdr lh on wl.from_location_id = lh.locn_id"
+ " left join locn_hdr lh2 on wl.to_location_id = lh2.locn_id"
+ "where wl.action in (:action)"
+ " and trunc(wl.date_updated) between :start_date and :end_date"
+ " and (ic.item_name in ('" + Join(Parameters!list.Value , "', '")" + ') OR"
+ " wl.source_updated = :username OR"
+ " wl.to_container in ('" + Join(Parameters!LPNList.Value , "', '")" + ') OR"
+ " (:list = '_N/A_' and :username is null and :LPNList = '_N/A_')"
+ " )"
+ " order by date_updated desc"
In this case you will need to provide default empty values to your lists. I used 'N/A' in my example.

Springboot Hibernate Query: filter by childrens attributes

Hi I have a problem generating a #query in my SpringBoot repo. Using hibernate.
My POJOs:
class Offer
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#ManyToMany
private List<Attribute> attributes;
#ManyToMany
private List<Offer> offers;
and
class Attribute
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
It works this way to get the "offer.offers by attribute". But now I want to enhance my query to get "offer offer.offers.attribute" an I strugle with it.
What I want is to find all Offers which have "Offer.attribute == 1" AND "all Offer.offers.attribute == 2"
Ending with this query:
#query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN o.offers.attributes ooa " <-- Problem issue
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 "
+ "AND a.id = 100 "
+ "AND ooa.id = 101 "
+ "GROUP BY o.id "
But upon compilation I get this exception:
org.hibernate.QueryException: illegal attempt to dereference collection
[offer0_.id.offers] with element property reference [attributes]
Now I wonder how to do it correctly?
Hope some SQL/Hibernate hero can help me.
Thanks in advance!
Gregor
Well... Iam one step further:
this query works:
#query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN oo.attributes ooa " <-- referencin JOIN
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 "
+ "AND a.id = 100 "
+ "AND ooa.id = 101 "
+ "GROUP BY o.id "
But now I get all results with at least on "Offers.offer.attributes == 101"
But I need all offers.offer having that attribute.
Any hints how to continue?

native query with spring data jpa doesn't return result

Hello I have written the following method in spring data jpa repo:
#RestResource(exported = false)
#Query(
value = "select q.* " +
"from question q " +
"left join question_program qp " +
"on q.question_id = qp.question_id " +
"where " +
"q.difficulty_level_id = ?1 " +
"and " +
"q.paid = false " +
"and " +
"qp.program_id = ?2 " +
"order by random() " +
"limit ?3",
nativeQuery = true
)
List<Question> getFreeRandomQuestions(
#Param("df") Integer df,
#Param("programId") Integer programId,
#Param("qs") Integer qs);
It is returning an empty list whereas when I run the same query in pgadmin I get all the desired rows. It also works in spring data jpa if I remove the join with question_program. What am I doing wrong?
UPDATE:
I have tried select q ... and select * .... Both have the same problem
UPDATE:
I kind of solved this by implementing a service which fetches that data using sessionFactory and raw sql query!.

Syntax error on multiple join statement

Hello, I'm getting a syntax error on this sql statement, can anyone advise thanks
String sql = "Select tblStudent.*,tblSchool.*,tblAgents.* " +
"FROM tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID='" + studentID + "'";
I was hoping that I could do multiple joins
But I am getting a syntax error.
For access, parenthesis with multiple joins means the following. If you have three joins, there are two left parenthesis after the from. The last join does not have a right parenthesis.
String sql = "Select tblStudent.*,tblSchool.*,tblAgents.* " +
"FROM (tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID)) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID='" + studentID + "'";
Access SQL injection has been covered in other threads .
String sql = "Select
tblStudent.StudentID,tblStudent.studentFirstName,"+
tblStudent.studentLastName,tblSchool.schoolName," +
tblAgents.agentFirstName,tblAgents.agentLastName " +
"FROM (tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID)) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID=#studentID";
I believe your final SQL should look like this:
SELECT tblStudent.*
,tblSchool.*
,tblAgents.*
FROM tblSchool
RIGHT JOIN (
tblAgents RIGHT JOIN tblStudent ON tblAgents.agentID = tblStudent.agentID
) ON tblSchool.schoolID = tblStudent.schoolID
WHERE tblStudent.StudentID=111;
So, the VBA code for creating this SQL should be
Dim sql As String
sql = "SELECT tblStudent.* ,tblSchool.* ,tblAgents.* " & _
"FROM tblSchool RIGHT JOIN (" & _
"tblAgents RIGHT JOIN tblStudent ON tblAgents.agentID = tblStudent.agentID" & _
") ON tblSchool.schoolID = tblStudent.schoolID " & _
"WHERE tblStudent.StudentID=" & studentID
Here I assume that studentID is numeric field. Also I would recommend to do not use * for selecting the data from more than one table, otherwise column names may be unpredictable and as mentioned in comments, it will require additional resources, which won't be used.

Hibernate Query doesn't work as expected

Hi i have the following Query:
String hql = "UPDATE Raumreservierung as rr " +
"set VON = :begin " +
"where VON = :Von " +
"and Raum_ID IN (SELECT r.ID FROM Raum r " +
"inner join r.Panel as pl with pl.ID = " + clientId + "";
IQuery query = CurrentSession.CreateQuery(hql);
query.SetParameter("begin", DateTime.Now);
query.SetParameter("Von", v.Von);
int result = query.ExecuteUpdate();
The Query do an Update on "VON". That works fine, but the rest of the Query is not working. It seems that the rest of the query is not working. But did not get any Error.
With the rest of the Query i mean the following part of the query:
"and Raum_ID IN (SELECT r.ID FROM Raum r " +
"inner join r.Panel as pl with pl.ID = " + clientId + "";
Because it should happen only a Update on the column "VON" for example when "clientId" is "AT2"
But that part is not working. Because the update happens also on other clientId.
You forgot to close your parentheses.
(Also, you should use a parameter for clientId too)