Possible Duplicate:
How to generate a dynamic “in (…)” sql list through Spring JdbcTemplate?
I'm trying to prepare a list of IDs for a IN()
clause in MsSQL in java. I've got the below code which looks like it should work, but it's throwing the error: java.sql.SQLException: Conversion failed when converting the nvarchar value '1,2' to data type int.
I'm kind of at a total loss as to why it's trying it like an integer when I'm passing a string. Any insight would be great.
I've also tried changing template.getId()
to template.getId().toString()
with no luck
//JOINs
Collection<Template> templates = search.getTemplateCollection();
if (templates != null && templates.size() > 0) {
searchQuery.append("INNER JOIN dbo.content_to_template ON content_to_template.template_id IN (:templateIds) AND content_to_template.content_id = content.id ");
StringBuilder templateIds = new StringBuilder();
for (Template template : templates) {
if (templateIds.length() == 0) {
templateIds.append(template.getId());
} else {
templateIds.append(",").append(template.getId());
}
}
queryMap.put("templateIds", templateIds.toString());
}
return this.namedjdbcTemplate.query(searchQuery.toString(), new MapSqlParameterSource(queryMap), this.contentRowMapper);