2012-09-15 16 views
9

La mia impostazione del progetto sono Spring MVC, Hibernate 3.2.x, il DB MySQLorg.hibernate.QueryParameterException: Impossibile trovare il nome del parametro

ottenere il seguente errore:

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

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

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

Errore:

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

classe AccountUtente:

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

risposta

1

Hibernate somewhy getta un'eccezione diversa da quella che deve essere .. Probabilmente, correggendo questo si dovrebbe sbarazzarsi del problema:

  • Rinomina i campi nella classe per seguire JavaConventions (dovrebbe iniziare con una lettera minuscola)
  • Usa semplice cl Nome culo invece di completo
0

Basta provare questo ....

Al posto di

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

uso

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

può essere che si aiuta a ...

1

Vedo che nella classe UserAccount, la proprietà perché l'indirizzo email è definito come "Email" e non "email".

Si consiglia di utilizzare la convenzione di denominazione Java quindi consiglierei di denominare la proprietà in "Posta elettronica" in Account utente.

19

Da quello che ricordo, questo è un caso di Hibernate che riporta il messaggio di errore sbagliato. Suppongo che l'errore reale sia "mappatura non trovata per com.cmgr.beans.UserAccount". Prova questa query:

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

Questo probabilmente ti mostrerà il messaggio di errore corretto. E una volta risolto, puoi cambiarlo per accettare i parametri.

2

Modificare I dati di

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

Dal momento che la classe UserAccount ha una proprietà di Email

3

Oggi ho avuto un problema simile ... mia entità non era nei pacchetti di scansione dalle primavera. Quindi Hibernate in qualche modo ha fatto qualcosa, ma il messaggio di errore era abbastanza confuso e non si adattava davvero al vero problema. Per risolvere il mio problema ho dovuto aggiungere le entità ai pacchettican.

+0

Ho avuto anche lo stesso problema: controlla il file di log accuratamente e dovresti vedere ibernato lamentarsi del fatto che non ha trovato un contesto di persistenza valido per il bean di entità dato! –

0

sostituire:

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

con:

@Autowired 
private String email; 

convenzioni di utilizzo java denominazione di denominazione delle variabili lettere minuscole & non hanno bisogno di scrivere

@Column(name = "email") 

perché è lo stesso nome della variabile nome come nome della colonna

Problemi correlati