我的DAO中包含以下代码,并且返回[Ljava.lang.String;无法转换为java.lang.String异常我想知道为什么这个原因和解决方案。
public Collection<Testdoc> findTestId(int idTest, String... testtype) {
StringBuffer buf = new StringBuffer("SELECT w FROM Testdoc w WHERE w.Test.idTest = :idTest");
buf.append("AND w.testtype IN ( :testtype ) ");
return findByQuery(buf.toString(), "idTest", idTest, "testtype ", testtype );
}
findByQuery
的最后一个参数似乎是String
类型。您已使用String[]
。String...
作为最后一个参数类型表示String[]
,但允许调用方使用fn("x", "y", "z")
之类的字符代替fn(new String[] { "x", "y", "z" })
。
@@ user85421指出findByQuery
可能被声明为类似以下内容:
Collection<Testdoc> findByQuery(String sql, String... args);
在这种情况下,您可能需要连接参数,尽管可能仍然不正确。然后您需要串联数组。如果没有
Arrays.concat
,请参阅问题How can I concatenate two arrays in Java?。