Hibernate异常 - 无法强制转换hibernate.internal.QueryImpl

时间:2013-05-02 22:49:58

标签: spring hibernate

我正在尝试使用数据库用户名进行身份验证。到目前为止,错误是:

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


}

    }

保存用户详细信息的数据库表

  /*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 {}.

1 个答案:

答案 0 :(得分:0)

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

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

另外,使用正确的绑定变量。现在你的查询是伪造的单引号,我不确定你是否写了一个拼写错误到堆栈溢出,但这样的事情会更安全:

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