13

我的项目设置是 MySQL DB 上的 Spring MVC、Hibernate 3.2.x

收到以下错误:

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 在 [from com.cmgr.beans.UserAccount u where 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;
    }
}
4

7 回答 7

21

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

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

这可能会向您显示正确的错误消息。一旦你解决了这个问题,你就可以改变它来接受参数。

于 2012-10-26T10:04:36.500 回答
3

今天我遇到了类似的问题......我的实体不在 Spring 的扫描包中。所以 Hibernate 不知何故做了一些事情,但错误消息非常混乱,并不真正适合真正的问题。为了解决我的问题,我必须将实体添加到 packagescan。

于 2014-05-12T12:12:24.477 回答
2

将您的查询更改为

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

由于您的 UserAccount 类具有Email

于 2012-10-26T12:44:25.287 回答
1

Hibernate 以某种方式抛出了与它必须的不同的异常.. 可能通过纠正这个你应该摆脱这个问题:

  • 重命名类中的字段以遵循 JavaConventions(应以小写字母开头)
  • 使用简单的类名而不是完全限定的
于 2012-09-15T04:31:37.897 回答
1

我看到在 UserAccount 类中,电子邮件地址的属性被定义为“电子邮件”而不是“电子邮件”。参考休眠文档

建议使用 java 命名约定,因此我建议您在 UserAccount 中将您的属性命名为“电子邮件”。

于 2012-09-19T04:55:12.840 回答
1

代替:

 @Autowired
    @Column(name = "email")
    private String Email;
    //

和:

@Autowired
private String email;

使用变量命名小写字母的java命名约定&不用写

@Column(name = "email")

因为变量名与列名同名

于 2016-12-06T10:06:51.707 回答
0

试试这个....

代替

List<UserAccount> result = currentSession().createQuery(hql)
 .setParameter("email", email)
 .list();

利用

List<UserAccount> result = currentSession().createQuery(hql)
 .setString("email", email)
 .list();

也许它会帮助你....

于 2012-09-18T13:30:36.893 回答