当前位置: 首页 > 知识库问答 >
问题:

查询参数异常: 找不到命名参数

百里飞捷
2023-03-14

我的项目设置是Spring MVC,Hibernate 3.2.x,在 MySQL 数据库上

收到以下错误:

org.hibernate.QueryParameterException:无法找到命名参数电子邮件

方法#1:

@Override
public Boolean isExist(String email) {
    boolean flag = false;  
    String hql = "from com.cmgr.beans.UserAccount u where u.email = :email";
    List<UserAccount> result = currentSession().createQuery(hql)
        .setParameter("email", email)
        .list();

    UserAccount userAccount = (UserAccount)result.get(0);
    if (userAccount!=null && userAccount.getEmail().equalsIgnoreCase(email)) {
        flag = true;
    }

    return flag;
}

方法#2:

 @Override
    public Boolean isExist(String email) {
        boolean flag = false;
        String hql = "from com.cmgr.beans.UserAccount u where u.email = :email";
        List<UserAccount> result = currentSession().createQuery(hql).setString("email", email).list();

        UserAccount userAccount = (UserAccount) result.get(0);
        if (userAccount != null && userAccount.getEmail().equalsIgnoreCase(email)) {
            flag = true;
        }
        return flag;
    }

错误:

java.lang.IllegalArgumentException:参数email不作为命名参数存在于[fromcom.cmgr.beans.UserAccountu其中u.email=: email]

用户帐户类:

package com.cmgr.beans;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

@Entity
@Table(name = "user_account")
public class UserAccount implements Serializable {

    @Autowired
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "user_account_seq")
    @SequenceGenerator(name = "user_account_seq", sequenceName = "user_account_seq")
    @Column(name = "user_id")
    private Long UserId = null;
    //
    @Autowired
    @Column(name = "user_name")
    private String UserName;
    //
    @Autowired
    @Column(name = "user_type")
    private String UserType = null;
    //
    @Autowired
    @Column(name = "first_name")
    private String FirstName;
    //
    @Autowired
    @Column(name = "last_name")
    private String LastName;
    //
    @Autowired
    @Column(name = "email")
    private String Email;
    //
    @Autowired
    @Column(name = "phone_contact_1")
    private String PhoneContact1 = null;
    //
    @Autowired
    @Column(name = "phone_contact_2")
    private String PhoneContact2 = null;
    //primary_address_is_usa
    @Autowired
    @Column(name = "primary_address_is_usa")
    private Boolean primaryAddressIsUsa = null;
    //
    @Autowired
    @Column(name = "address_1")
    private String Address1 = null;
    //
    @Autowired
    @Column(name = "city1")
    private String city1 = null;
    //
    @Autowired
    @Column(name = "state1")
    private String state1 = null;
    //
    @Autowired
    @Column(name = "country1")
    private String country1 = null;
    //
    @Autowired
    @Column(name = "zipcode")
    private Integer zipcode = 0;
    //
    @Autowired
    @Column(name = "Industry")
    private String Industry = null;
    //is the user account Active either due to user deactivation,admin deactivation, or nonpayment
    @Autowired
    @Column(name = "active")
    private boolean Active = false;
    //1 to 1 relation with registerationCode in Registeration class 
    @Autowired
    @Qualifier("UserRegisteration")
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Registeration_Code_fk", referencedColumnName = "registeration_code", nullable = false)
    private UserRegisteration UserRegisteration;
    //1 to many relation with EmailId in Email class  
    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "emailed_id")
    private List<Emailed> emailed = null;
    //1 to many relation with signatureId in signature class
    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "signature_id")
    private List<Signature> signatures;
    //1 to many relation with UserAccountDocId in UserAccountDoc class 
    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "user_doc_id")
    private Set<UserAccountDocumentRelationship> UserAccountDocumentRelationship;

    @Autowired(required = false)
    public UserAccount() {
    }

    @Autowired(required = true)
    public UserAccount(String UserName, String FirstName, String LastName, String Email, String Industry) {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Email = Email;
        this.Industry = Industry;
        this.UserName = UserName;
    }

    @Autowired(required = false)
    public UserAccount(String UserName, Long UserId, String FirstName, String LastName, String Email, String Industry) {
        this.UserId = UserId;
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Email = Email;
        this.Industry = Industry;
        this.UserName = UserName;
    }

    public String getIndustry() {
        return Industry;
    }

    public void setIndustry(String Industry) {
        this.Industry = Industry;
    }

    public String getAddress1() {
        return Address1;
    }

    public void setAddress1(String Address1) {
        this.Address1 = Address1;
    }

    public String getPhoneContact1() {
        return PhoneContact1;
    }

    public void setPhoneContact1(String PhoneContact1) {
        this.PhoneContact1 = PhoneContact1;
    }

    public String getPhoneContact2() {
        return PhoneContact2;
    }

    public void setPhoneContact2(String PhoneContact2) {
        this.PhoneContact2 = PhoneContact2;
    }

    public boolean isActive() {
        return Active;
    }

    public void setActive(boolean Active) {
        this.Active = Active;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }

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

    public com.cmgr.beans.UserRegisteration getUserRegisteration() {
        return UserRegisteration;
    }

    public void setUserRegisteration(com.cmgr.beans.UserRegisteration UserRegisteration) {
        this.UserRegisteration = UserRegisteration;
    }

    public Long getUserId() {
        return UserId;
    }

    public void setUserId(Long UserId) {
        this.UserId = UserId;
    }

    public String getUserType() {
        return UserType;
    }

    public void setUserType(String UserType) {
        this.UserType = UserType;
    }

    public List<Emailed> getEmailed() {
        return emailed;
    }

    public void setEmailed(List<Emailed> emailed) {
        this.emailed = emailed;
    }

    public List<Signature> getSignatures() {
        return signatures;
    }

    public void setSignatures(List<Signature> signatures) {
        this.signatures = signatures;
    }

    public String getCity1() {
        return city1;
    }

    public void setCity1(String city1) {
        this.city1 = city1;
    }

    public String getCountry1() {
        return country1;
    }

    public void setCountry1(String country1) {
        this.country1 = country1;
    }

    public Boolean getPrimaryAddressIsUsa() {
        return primaryAddressIsUsa;
    }

    public void setPrimaryAddressIsUsa(Boolean primaryAddressIsUsa) {
        this.primaryAddressIsUsa = primaryAddressIsUsa;
    }

    public String getState1() {
        return state1;
    }

    public void setState1(String state1) {
        this.state1 = state1;
    }

    public Integer getZipcode() {
        return zipcode;
    }

    public void setZipcode(Integer zipcode) {
        this.zipcode = zipcode;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String UserName) {
        this.UserName = UserName;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UserAccount other = (UserAccount) obj;
        if ((this.UserId == null) ? (other.UserId != null) : !this.UserId.equals(other.UserId)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 73 * hash + (this.UserId != null ? this.UserId.hashCode() : 0);
        return hash;
    }

    public Set<com.cmgr.beans.UserAccountDocumentRelationship> getUserAccountDocumentRelationship() {
        return UserAccountDocumentRelationship;
    }

    public void setUserAccountDocumentRelationship(Set<com.cmgr.beans.UserAccountDocumentRelationship> UserAccountDocumentRelationship) {
        this.UserAccountDocumentRelationship = UserAccountDocumentRelationship;
    }
}

共有3个答案

胡致远
2023-03-14

将查询更改为

String hql = "from com.cmgr.beans.UserAccount u where u.Email = :email";

由于您的UserAccount类具有Email属性

许天逸
2023-03-14

今天我有一个类似的问题...我的实体不在Spring扫描的包中。因此,Hibernate以某种方式做了一些事情,但是错误消息非常令人困惑,并且不适合真正的问题。为了解决我的问题,我不得不将实体添加到软件包扫描中。

南门志
2023-03-14

据我所知,这是Hibernate报告错误错误消息的情况。我猜,实际错误是“找不到com.cmgr.beans.UserAccount的映射”。试试这个查询:

String hql = "from com.cmgr.beans.UserAccount";

这可能会向您显示正确的错误消息。修复后,您可以更改它以接受参数。

 类似资料:
  • 请找到我使用过的代码。以下HQL查询失败,说明: 找不到命名参数 [模板 Id] 但是模板 Id 存在于我的模型类中。 请帮助解决问题或可能导致此类错误的原因: 模型文件 请帮助解决我的问题

  • 我有以下查询: 执行查询时,我得到以下异常: java.lang.非法参数异常: 组织.Hibernate.查询参数异常: 找不到命名参数 [startDate] 您能告诉我的查询有什么问题吗?

  • 我有一个疑问 但是在使用hibernate执行查询时,我收到 org.hibernate.QueryParameterException:无法定位命名参数[age] 怎么了?

  • 问题内容: 谁能指出我如何将order by子句作为命名参数传递给HQL? 有效的示例: 无效的示例: 问题答案: 不支持,只能在and 子句中使用输入参数,并且不能为子句使用参数。或者,如果我改写,您不能对列使用参数,只能对值使用。因此,要么: 有尽可能多的命名查询排序顺序 将排序字符串连接到查询字符串 使用条件查询

  • 问题内容: 嗨,我有一个命名查询 我想这样设置限制: 但这在服务器启动时显示错误。我正在使用以下代码在DAO类中调用查询: 需要设置开始和结束参数。请帮忙。 问题答案: 正如@DataNucleus所说,LIMIT在JPQL中不是有效的关键字。这不是指定要返回多少行的方法。这是您的操作方式: 这将是调用命名查询的代码:

  • 错误:运算符不存在:uuid=bytea 有什么建议吗?