0

我已经盯着这个问题太久了。当执行 .list() 行时,sp​​ring 会抛出关于 .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;
}


}
4

1 回答 1

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()]) ))
于 2013-10-14T18:52:34.737 回答