2013-05-02 92 views
0

我试图用数据库用户名进行认证。到目前为止,错误是:Hibernate异常 - hibernate.internal.QueryImpl无法转换

Your login attempt was not successful, try again. 

    Reason: org.hibernate.internal.QueryImpl cannot be cast to com.**.**.model.UserEntity 

在DAO类查询

@Repository 
public class UserEntityDAOImpl implements UserEntityDAO{ 


@Autowired 
private SessionFactory sessionFactory; 

public void setSessionFactory(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
} 

public Session getCurrentSession() { 
    return this.sessionFactory.getCurrentSession(); 
} 

    @Override 
public UserEntity getUserByName(String username) { 
    // TODO Auto-generated method stub 
UserEntity userEntity = (UserEntity) 

    sessionFactory.getCurrentSession().createQuery(


    "select u from UserEntity u where u.username = '' + username + ''"); 

      return userEntity; 
} 

服务

@Service("customUserDetailsService") 
public class CustomUserDetailsService implements UserDetailsService{ 

@Autowired 
private UserEntityDAO userEntityDAO; 

@Autowired 
private Assembler assembler; 

@Override 
@Transactional(readOnly = true) 
public UserDetails loadUserByUsername(String username) 
     throws UsernameNotFoundException { 
    // TODO Auto-generated method stub 
    UserDetails userDetails = null; 

    UserEntity userEntity = userEntityDAO.getUserByName(username); 

    if (userEntity == null) 

    throw new UsernameNotFoundException("user not found"); 

    return assembler.buildUserFromUser(userEntity); 


} 

    } 
保存用户信息

DB表

/*Table structure for table `user` */ 
    CREATE TABLE `user` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT , 
`name` VARCHAR(45) NULL DEFAULT NULL , 
`password` VARCHAR(45) NOT NULL , 
`username` VARCHAR(45) NOT NULL , 
`active` TINYINT(1) NOT NULL , 
PRIMARY KEY (`user_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; 

型号

@Entity 
@Table(name = "user", schema = "") 
@Component 
public class UserEntity implements Serializable { 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name = "user_id") 
private Integer id; 

@Column(name = "name") 
private String name; 

@Basic(optional = false) 
@Column(name = "password") 
private String password; 

@Basic(optional = false) 
@Column(name = "username") 
private String username; 

@Basic(optional = false) 
@Column(name = "active") 
private boolean active; 

@JoinTable(name = "user_role", joinColumns = { 
@JoinColumn(name = "user_id")}, inverseJoinColumns = { 
@JoinColumn(name = "role_id")}) 
@OneToMany 
private Set <Role> roles; 

public UserEntity() { 

} 

    //getters and setters 

我想知道的是为什么查询有问题,为什么用户名不能从数据库中检索。

编辑:更改查询后,登录仍然不成功。登录页面被返回,除此之外,输出控制台中没有错误消息:

Hibernate: select userentity0_.user_id as user1_1_, userentity0_.active as 

    active1_, userentity0_.name as name1_, userentity0_.password as password1_, 

    userentity0_.username as username1_ from user userentity0_ where 

    userentity0_.username=? 


    Hibernate: select roles0_.user_id as user1_1_1_, roles0_.role_id as role2_2_1_, 


    role1_.role_id as role1_0_0_, role1_.role as role0_0_ from user_role roles0_ inner 

    join role role1_ on roles0_.role_id=role1_.role_id where roles0_.user_id=? 


    INFO : com.**.**.controller.ApplicationController - This is the login page {}. 
+0

可怕的缩进......你能确保当你提问时你的代码看起来很好吗? – 2013-05-03 01:54:24

+0

并尝试隔离与问题相关的部分。粘贴服务代码没有意义。 – 2013-05-03 01:58:40

+0

嗨我很抱歉乱码显示。我试图修改它。 – user2259555 2013-05-03 02:09:51

回答

0

您忘记执行您创建的查询。它应该是:

sessionFactory.getCurrentSession().createQuery(...).uniqueResult(); 

此外,请使用适当的绑定变量。因为它代表你的查询是假的单引号,我不知道,如果你犯了一个错字将其粘贴到计算器,但这样的事情会更安全:

sessionFactory 
    .getCurrentSession() 
    .createQuery("select u from UserEntity u where u.username = :username") 
    .setParameter("username", username) 
    .uniqueResult(); 
+0

是的,我省略了添加.uniqueResult。我现在已经更改了查询,但是现在当我输入用户详细信息时,除了在堆栈跟踪中显示的HQL查询外,没有任何事情发生。我错过了什么吗? – user2259555 2013-05-03 01:23:42

+0

查询不显示堆栈跟踪。有一个异常会导致显示堆栈跟踪。您应该提供例外信息 – 2013-05-03 01:56:18

+0

嗨,感谢您的回复。输出或页面中没有异常信息。登录页面仍在屏幕上,HQL选择查询显示在控制台中。我已经添加到我的第一篇文章。 – user2259555 2013-05-03 02:09:14

相关问题