Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
org.hibernate.QueryException:未设置所有命名参数_Hibernate_Jpa_Jpa 2.0_Criteria Api - Fatal编程技术网

org.hibernate.QueryException:未设置所有命名参数

org.hibernate.QueryException:未设置所有命名参数,hibernate,jpa,jpa-2.0,criteria-api,Hibernate,Jpa,Jpa 2.0,Criteria Api,以Hibernate 3.5.1-Final作为提供程序,我从JPA 2.0 Criteria API中获得了极其奇怪的行为 我正在尝试构建一个动态查询,在JPQL中如下所示: SELECT e FROM Employee e WHERE lower(e.firstName) like lower(:employeeName) OR lower(e.lastName) like lower(:employeeName) 但是不断地得到错误 java.lang.IllegalArgu

以Hibernate 3.5.1-Final作为提供程序,我从JPA 2.0 Criteria API中获得了极其奇怪的行为

我正在尝试构建一个动态查询,在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 + "]";
}
@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;
}
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();
}
}

EmployeeService.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 + "]";
}
@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;
}
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();
}
公共列表findByCriteria(字符串employeeName、字符串officeName、,
int pageNumber,int pageSize){
CriteriaBuilder cb=entityManager.getCriteriaBuilder();
CriteriaQuery c=cb.createQuery(Employee.class);
根emp=c.from(Employee.class);
c、 选择(emp);
c、 订购人(cb.asc(emp.get(“firstName”));
列表条件=新建ArrayList();
ParameterExpression paramEmployeeName=cb.parameter(String.class);
//使用参数构建标准
如果(!StringUtils.isEmpty(employeeName)){
标准。添加(cb.or)(
cb.like(cb.lower(emp.get(“firstName”)),cb.lower(parametemployeename)),
类cb.like(cb.lower(emp.get(“lastName”)),cb.lower(paramEmployeeName));
}
//将条件添加到CriteriaQuery
c、 其中(criteria.toArray(新谓词[0]);
TypedQuery=entityManager.createQuery(c);
如果(!StringUtils.isEmpty(employeeName)){
query.setParameter(参数employeeName,“%”+employeeName+“%”);
}
页面(查询、页码、页面大小);
返回query.getResultList();
}

您需要执行以下操作

SELECT e FROM Employee e WHERE lower(e.firstName) like :employeeName
String employeename = employeename.toLowerCase();
无论你把雇员的名字定在哪里

执行以下操作

SELECT e FROM Employee e WHERE lower(e.firstName) like :employeeName
String employeename = employeename.toLowerCase();

您需要执行以下操作

SELECT e FROM Employee e WHERE lower(e.firstName) like :employeeName
String employeename = employeename.toLowerCase();
无论你把雇员的名字定在哪里

执行以下操作

SELECT e FROM Employee e WHERE lower(e.firstName) like :employeeName
String employeename = employeename.toLowerCase();

这可以使用位置参数来代替。如果将
:employeeName
替换为?在这两种情况下,然后使用:

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

这可以使用位置参数来代替。如果将
:employeeName
替换为?在这两种情况下,然后使用:

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

尽管查询中的Javadoc确实指出命名参数可能在查询中出现多次。尽管查询中的Javadoc确实指出命名参数可能在查询中出现多次。您确定调用参数“employeeName”不为null或空的方法吗?您仅在该条件内设置参数。是否确实调用了参数“employeeName”不为null或空的方法?只能在该条件内设置参数。