Here\'s what I need to be able to do.
I have a List in java which I can convert to comma separate string of IDs like so \"3,4,5,6,1,2\"
I wonder if there\'s way
If you can modify the query in java, you could do something like this:
SELECT t.id
FROM t_test t
ORDER BY DECODE(t.id, 3, 'A', 'B') ASC,
DECODE(t.id, 4, 'A', 'B') ASC,
DECODE(t.id, 5, 'A', 'B') ASC,
DECODE(t.id, 6, 'A', 'B') ASC,
DECODE(t.id, 1, 'A', 'B') ASC,
DECODE(t.id, 2, 'A', 'B') ASC;
You have to put a decode in the order by clause for each element in the list. The second parameter in each decode is one element of the list.
select t1 from (select t.id t1 from t_test t order by t.id asc);
In hibernate u can do -
public String getResult(String sortOrder){
SQLQuery query = getSession().createSQLQuery("select t from ( select t.id t from t_test t order by t.id=:sortOrder").addScalar("name", Hibernate.STRING);
query.setString("sortOrder", sortOrder);
return (String)query.uniqueResult();
}
I don't think it's possible. You can only use ascending or descending order in queries. But what you can do is to use a prepared statement like this select * from t_test t where t.id = ?
and run this for each ID in your list and add the result to a result list.
You could also try to do this with a stored procedure whose parameter would be the list of IDs
EDIT
Another idea is to use IN operator on small chunks of your ID-list (maybe 10 in each). As this will return results in unspecified order, you could write a custom comparator or another class that brings this list into your specified order. Then you can concatenate all sub-lists into one result list. For a list with 100 entries and batch size 10 you would only need 10 DB queries + some time for reordering
Something like this:
with ordered_ids as (
select to_number(regexp_substr ('3,4,5,6,1,2','[^,]+',1,level)) as id, level as sort_order
from dual
connect by regexp_substr ('3,4,5,6,1,2','[^,]+',1,level) is not null
)
select t.id
from t_test t
join ordered_ids oi on oi.id = t.id
order by oi.sort_order;
You can probably make the literal '3,4,5,6,1,2'
a parameter for a PreparedStatement but I haven't tested that.