Stefan Spajic
Stefan Spajic

Reputation: 1

Could not locate named parameter Hibernate

This is my dao method in which i try to get user details from database :

public UserInfo findUserInfo(String userName) {
    String sql = "Select new " + UserInfo.class.getName() + "(u.username,u.password) "//
            + " from " + User.class.getName() + " u where u.username = :username ";

    Session session = sessionFactory.getCurrentSession();

    Query query = session.createQuery(sql);
    query.setParameter("username", userName);

    return (UserInfo) query.uniqueResult();
}

And when i try to execute it i get this error :

Caused by: org.hibernate.QueryParameterException: could not locate named parameter [username]
    at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:148)
    at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:165)
    at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:523)
    at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:493)
    at org.spajic.stefan.springhibernatesecurity.dao.UserInfoDAO.findUserInfo(UserInfoDAO.java:33)
    at org.spajic.stefan.springhibernatesecurity.dao.UserInfoDAO$$FastClassBySpringCGLIB$$c14aad39.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at org.spajic.stefan.springhibernatesecurity.dao.UserInfoDAO$$EnhancerBySpringCGLIB$$3fdca957.findUserInfo(<generated>)
    at org.spajic.stefan.springhibernatesecurity.authentication.MyUserDetailsService.loadUserByUsername(MyUserDetailsService.java:25)
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:114)
    ... 36 more

And here is User class and UserInfo class as well. User is hibernate entity and UserInfo is model that i use.

@Entity
@Table(name = "Users")
public class User {

    private String username;
    private String password;
    private boolean enabled;

    @Id
    @Column(name = "username", length = 36, nullable = false)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "password", nullable = false)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "enabled", nullable = false)
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

}

public class UserInfo {

   private String userName;
   private String password;

   public UserInfo()  {

   }

   // Do not change this constructor, it used in hibernate Query.
   public UserInfo(String userName, String password) {
       this.userName = userName;
       this.password = password;
   }

   public String getUserName() {
       return userName;
   }

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

   public String getPassword() {
       return password;
   }

   public void setPassword(String password) {
       this.password = password;
   }

}

Any help ?

Upvotes: 0

Views: 1784

Answers (2)

Frank
Frank

Reputation: 881

Hope this helps , query accept even positional parameters :

maybe you can try u.username = ?1 in query
and query.setParameter(1, userName); setting parameter

Anyway you can get the result to the User object and use that , i think you don't need UserInfo .

Upvotes: 0

Farooq Khan
Farooq Khan

Reputation: 580

Looks like the property is named UserInfo:userName in your POJO and your are trying to use username

I mean you are using a small 'n' instead of a caps 'N'

Upvotes: 2

Related Questions