2010-09-26 29 views
1

從Hibernate 3.5.1-Final作爲提供者的JPA 2.0 Criteria API中,我遇到了非常奇怪的行爲。org.hibernate.QueryException:並非所有命名參數都已設置

我試圖建立一個看起來像這樣在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 + "]"; 
} 

}

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

}

僱員Service.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(); 
} 
+1

你確定你調用方法與參數「employeeName」不是爲空或空?您只能在該條件下設置參數。 – djmj 2012-06-19 01:54:54

回答

0

你需要做以下

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

和徘徊無論你設置employeename

做以下

String employeename = employeename.toLowerCase(); 
1

這可能工作使用位置參數。如果您將:employeeName替換爲?在這兩種情況下,然後使用:

query.setParameter(0, "name"); 
query.setParameter(1, "name"); 
+0

雖然查詢中的Javadoc確實聲稱一個命名參數可能會在查詢中出現多次。 – 2012-06-19 01:53:27

相關問題