Question

I've been staring at this issue for too long. When executing the .list() line spring throws an exception with regards to the .add(Restrictions.in("LocationCode", locations)) java.util.Arrays$ArrayList cannot be cast to java.lang.String.

Restrictions.in is expecting (string, Object[]) no??

Can someone point out what I'm missing when passing in to the 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();

        }

Customer class

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;
}


}
Was it helpful?

Solution

Semi-false positive on what the issue was, the following works(I need to refactor, but hopefully the gist is understood):

    String[] locations=SelectedLocation.split(",");
    List<String> locs=new ArrayList<String>();
    for(String l:locations)
    {
    locs.add(l);
    }

...

The exception was actually referring to:

.add(Restrictions.in("Division", SelectedDivision))

(adding this for completeness)

.add(Restrictions.in("LocationCode", locs.toArray(new String[locs.size()]) ))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top