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

发布于 2024-09-25 03:49:30 字数 4373 浏览 3 评论 0原文

我从使用 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 + "]";
}

}

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

}

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

I'm getting extremely weird behavior out of JPA 2.0 Criteria API with Hibernate 3.5.1-Final as a provider.

I'm trying to build a dynamic query that looks like this in JPQL:

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

but keep getting the error

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]    

If I take away the path for lastName it works fine. Am I doing something wrong? Below are the classes and query involved. Thanks!

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

}

EmployeeService.java method:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-10-02 03:49:30

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

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

This may work using positional parameters instead. If you replace the :employeeName with ? in both cases and then use:

 query.setParameter(0, "name");
 query.setParameter(1, "name");
无所谓啦 2024-10-02 03:49:30

您需要执行以下操作

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

,并且无论您在何处设置员工名称,

都执行以下操作

String employeename = employeename.toLowerCase();

You need to do the following

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

and whereever you set employeename

do the following

String employeename = employeename.toLowerCase();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文