这是我的dao方法,我尝试从数据库中获取用户详细信息:
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();
}
当我尝试执行它时,我收到此错误:
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
这里也是User类和UserInfo类。用户是hibernate实体,UserInfo是我使用的模型。
@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;
}
}
任何帮助?
答案 0 :(得分:2)
看起来该属性在您的POJO中被命名为UserInfo:userName,并且您正在尝试使用用户名
我的意思是你使用的是小'n'而不是'N'
答案 1 :(得分:0)
希望这有帮助,查询甚至接受位置参数:
也许您可以在查询中尝试u.username = ?1
和query.setParameter(1, userName);
设置参数
无论如何,您可以将结果发送到用户对象并使用它,我认为您不需要 UserInfo 。