简体   繁体   中英

org.hibernate.QueryParameterException: could not locate named parameter

My project setting are Spring MVC, Hibernate 3.2.x, on MySQL DB

Getting the following error:

org.hibernate.QueryParameterException: could not locate named parameter email

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

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

Error:

java.lang.IllegalArgumentException: Parameter email does not exist as a named parameter in [from com.cmgr.beans.UserAccount u where u.email =:email]

UserAccount class:

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

From what i remember, this is a case of Hibernate reporting the wrong error message. I guess, the actual error is "mapping not found for com.cmgr.beans.UserAccount". Try this query:

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

This probably will show you the correct error message. And once you fix that, you can change it to accept the parameters.

Today i had a similar issue... My entity was not in the scanned packages from Spring. So Hibernate somehow did something but the error message was quite confusing and did not really fit to the real issue. To solve my problem i had to add the entities to the packagescan.

Change your query to

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

Since your UserAccount class has a property of Email

Hibernate somewhy throws different exception than it has to be.. Probably by correcting this you should get rid of the problem:

  • Rename fields in the class to follow JavaConventions (should start with a small letter)
  • Use simple class name instead of fully qualified

I see that in the UserAccount class, the property for the email address is defined as 'Email' and not 'email'. Refer to the hibernate documentation

It is advisable to use java naming convention hence I would advice you to name your property to 'email' in UserAccount.

Just Try this....

In place of

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

use

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

may be it will helps you....

replace:

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

with:

@Autowired
private String email;

use java naming conventions of variable naming small letters & don't need to write

@Column(name = "email")

because variable name is same name as column name

Verify that in the SQL query the parameter starts with ':'

Example: WHERE NAME =:pMyParameter

At the same time, you must take into account that in the Java code the parameter must not be named with ':'

Example: parameters.put("pMyParameter", myParameter);

may be it will helps you...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM