This question already has answers here:
Hibernate Criterion IN Clause 1000 break up
(3 answers)
Java Oracle exception - "maximum number of expressions in a list is 1000"
(7 answers)
Closed 5 years ago.
I am constructing a hibernate query and I pass a list of String values into the IN clause. That list sometimes happens to be more than 1000 values so I receive an error. I have looked up some solutions like breaking that clause into smaller ones or making temporary table, but none of them showed how actually it is better to work with variable list.
So if I had something like:
SELECT * FROM MY_TABLE WHERE NAME IN (list).
What would be the best way to handle this?
Related
This question already has answers here:
PreparedStatement IN clause alternatives?
(33 answers)
Closed 3 years ago.
i have this query in spring:
private String SQL_Clear_Deleted_Options = "DELETE FROM vote_votes WHERE poll_id=? AND option_id NOT IN ?"
my problem is with second ?. the correct form would be (id1,id2,id3,...). how can i pass a string like cl="0,1,2,3,6" to this query?
i'm using jdbcTemplate. so it would be
jdbcTemplate.update(SQL_Clear_Deleted_Options, id,cl)
what should be cl?
You can use setArray of PreparedStatement to set multiple values.
https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setArray(int,%20java.sql.Array)
And the array should be of the correct type, so you could create it with the Connection.createArrayOf function:
https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#createArrayOf(java.lang.String,%20java.lang.Object[])
You can see a lot of discussions about that topic on SO already, e.g.:
How to use an arraylist as a prepared statement parameter
Edit: Forgot to mention: The JdbcTemplate should set the Parameter correctly when the Array is given as parameter.
This question already has answers here:
Postgres query to check a string is a number
(8 answers)
Closed 5 years ago.
I have column in my database table named 'anwers' of type 'character'. Data may be numbers or strings that's why I used column of type 'character'. And I have to calculate the sum of anweres if it is a numeric. But I found that ISNUMERIC will not work for column type of text/character .
How can I solve this issue?
In DB2, I've done something like UCASE(answers) = LCASE(answers) to test if it's numeric. This could work if your column is simple letters/numbers, not including punctuation characters.
This question already has answers here:
How to load a large number of strings to match with oracle database?
(3 answers)
Closed 6 years ago.
0I have this method:
public List<Product> getProductsByListIds(List<Long> ids) {
String query = "from Product pr where pr.id in(:ids)";
List<Product> products= (List<Product>) getSession().createQuery(query)
.setParameterList("ids", ids).list();
return products;
}
This method is OK, my only problem is when the ids.size() >1000
I'm trying to find a convincing solution to this problem.
My advice would be to take a step back and look at the design and what you're trying to achieve, passing hundreds of parameters into an SQL statement is not going to be very efficient and I'd be surprised if it's the most elegant solution to your requirement.
Without knowing more about how this method is called and where this list of ids comes from it's difficult to give concrete advice, however I would recommend that you look into using joins if possible.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Remove Duplicates from an Array?
String={3333,4444,5555,5555,1111}
Using Delimiter i have put the String values in an array[]..
Now, How can i remove the same entries in an Array???
Use Set instead, Or use temporarily Set and get the unique result back to array
See
What interface in Java can I use to store unique objects?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
java PreparedStatement
Can I make the prepared statement
SELECT * FROM STUDENTS WHERE STUDENT_ID IN ?
into
SELECT * FROM STUDENTS WHERE STUDENT_ID IN (1,2,3)
Given that the collection of student ids is a string array.
I think your best solution is going to be generate the in clause dynamically: IN (?,?,?,?) , and then calling preparedStatement.setInt(i + 1, myValues[i]) for each value in your array/collection. The bad news is you will end up with a different preparedStatement for each time you have a different number of values.
See duplicate question: PreparedStatement IN clause alternatives?