org.hibernate.QueryException: установлены не все именованные параметры

Я получаю чрезвычайно странное поведение от JPA 2.0 Criteria API с Hibernate 3.5.1-Final в качестве поставщика.

Я пытаюсь создать динамический запрос, который выглядит так в JPQL:

 SELECT e FROM Employee e WHERE lower(e.firstName) like lower(:employeeName) OR lower(e.lastName) like lower(:employeeName)     

но продолжаю получать ошибку

java.lang.IllegalArgumentException: org.hibernate.QueryException: Not all named parameters have been set: [param0] [select generatedAlias0 from com.company.model.Employee as generatedAlias0 where ( lower(generatedAlias0.firstName) like lower(:param0) ) or ( lower(generatedAlias0.lastName) like lower(:param1) ) order by generatedAlias0.firstName asc]    

Если я уберу путь для lastName, он отлично работает. Я делаю что-то неправильно? Ниже приведены используемые классы и запросы. Спасибо!

AbstractEntity.java:

@MappedSuperclass
@SuppressWarnings("serial")
public abstract class AbstractEntity<ID extends Serializable> extends AbstractBaseEntity
    implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(nullable = false)
protected ID id;

/**
 * Gets the identifier of the entity.
 * 
 * @since 0.0.1
 * @return the identifier
 */
public ID getId() {
    return this.id;
}

/**
 * Sets the identifier of the entity
 *
 * @since 0.0.1
 * @param id the identifier
 */
public void setId(ID id) {
    this.id = id;
}

/**
 * Determines if the entity is new by checking if the Id is null
 * 
 * @since 0.0.1
 * @return true if id == null
 */
public boolean isNew() {
    return id == null;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public String toString() {
    return this.getClass().toString() + "[id=" + id + "]";
}

}

Сотрудник.java:

@Entity
public class Employee  {

@Basic(optional = false)
@Column(nullable = false, length = 50)
private String firstName;

@Basic(optional = false)
@Column(nullable = false, length = 50)
private String lastName;

public Employee() {
}

public Employee(Integer id) {
    this.id = id;
}

public Employee(Integer id, String firstName, String lastName) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
}

public Employee(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Employee)) {
        return false;
    }
    Employee other = (Employee) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

}

Метод EmployeeService.java:

public List<Employee> findByCriteria(String employeeName, String officeName,
        int pageNumber, int pageSize) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> emp = c.from(Employee.class);
    c.select(emp);
    c.orderBy(cb.asc(emp.get("firstName")));

    List<Predicate> criteria = new ArrayList<Predicate>();
    ParameterExpression<String> paramEmployeeName = cb.parameter(String.class);
    //Build the criteria with parameters
    if (!StringUtils.isEmpty(employeeName)) {

        criteria.add(cb.or(
                cb.like(cb.lower(emp.<String>get("firstName")), cb.lower(paramEmployeeName)),
                cb.like(cb.lower(emp.<String>get("lastName")), cb.lower(paramEmployeeName))));
    }
    //Add the criteria to the CriteriaQuery
    c.where(criteria.toArray(new Predicate[0]));

    TypedQuery<Employee> query = entityManager.createQuery(c);
    if (!StringUtils.isEmpty(employeeName)) {
        query.setParameter(paramEmployeeName, "%" + employeeName + "%");
    }

    page(query, pageNumber, pageSize);

    return query.getResultList();
}

person dukethrash    schedule 26.09.2010    source источник
comment
Вы уверены, что вызвали метод с аргументом employeeName, который не является нулевым или пустым? Вы только устанавливаете параметр внутри этого условия.   -  person djmj    schedule 19.06.2012


Ответы (2)


Вместо этого это может работать с использованием позиционных параметров. Если заменить :employeeName на ? в обоих случаях, а затем используйте:

 query.setParameter(0, "name");
 query.setParameter(1, "name");
person Alex Barnes    schedule 19.06.2012
comment
Хотя в Javadoc on Query говорится, что именованные параметры могут появляться в запросе несколько раз. - person Alex Barnes; 19.06.2012

Вам нужно сделать следующее

SELECT e FROM Employee e WHERE lower(e.firstName) like :employeeName

и везде, где вы устанавливаете имя сотрудника

сделать следующее

String employeename = employeename.toLowerCase();
person user373201    schedule 29.12.2010