简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM