Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 711
cyber101 Avatar asked Sep 15 '12 00:09

cyber101


2 Answers

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.

like image 84
Jemish Patel Avatar answered Oct 05 '22 17:10

Jemish Patel


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.

like image 44
djnose Avatar answered Oct 05 '22 18:10

djnose