我一直盯着这个问题太久了。执行.list()行时,spring会抛出一个关于.add的异常(Restrictions.in(“LocationCode”,locations)) java.util.Arrays $ ArrayList无法强制转换为java.lang.String。
Restrictions.in期待(string,Object [])no ??
有人可以指出我在传入Restrictions.in时错过了什么吗?
public List getIDsByDivision(String SelectedAccountCode,
List SelectedDivision, String SelectedLocation) {
List IDs=null;
String result="--Select--";
Session session=null;
String[] locations=SelectedLocation.split(",");
try
{
session=sessionFactory.openSession();
if(null!=SelectedAccountCode && !SelectedAccountCode.equals("--Select--")
&&SelectedDivision.size()>0&&!SelectedDivision.equals("--Select--")
&&locations.length>0&&!SelectedLocation.equals("--Select--"))
{
IDs=session.createCriteria(Customer.class)
// TODO fix this to location
.setProjection(Projections.distinct(Projections.property("ID")))
.add(Restrictions.eq("AcctCode", SelectedAccountCode))
.add(Restrictions.eq("Division", SelectedDivision))
.add(Restrictions.in("LocationCode", locations ))
.list();
}
客户类
package <removed>;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Customer {
@Id
private String ID;
@Column
private String Name;
private String AcctCode;
private String LocationCode;
private String Division;
private String ContactEmail;
public Customer(){}
public Customer(String iD, String name, String acctCode,
String locationCode, String division, String contactEmail) {
super();
ID = iD;
Name = name;
AcctCode = acctCode;
LocationCode = locationCode;
Division = division;
ContactEmail=contactEmail;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAcctCode() {
return AcctCode;
}
public void setAcctCode(String acctCode) {
AcctCode = acctCode;
}
public String getLocationCode() {
return LocationCode;
}
public void setLocationCode(String locationCode) {
LocationCode = locationCode;
}
public String getDivision() {
return Division;
}
public void setDivision(String division) {
Division = division;
}
public String getContactEmail() {
return ContactEmail;
}
public void setContactEmail(String contactEmail) {
ContactEmail = contactEmail;
}
}
答案 0 :(得分:0)
关于问题的半假性积极,下面的工作(我需要重构,但希望理解要点):
String[] locations=SelectedLocation.split(",");
List<String> locs=new ArrayList<String>();
for(String l:locations)
{
locs.add(l);
}
...
该例外实际上指的是:
.add(Restrictions.in("Division", SelectedDivision))
(为了完整性而添加此内容)
.add(Restrictions.in("LocationCode", locs.toArray(new String[locs.size()]) ))