0

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

  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 {}.
4

1 回答 1

0

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

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

此外,使用适当的绑定变量。就目前而言,您的查询是带有单引号的虚假查询,我不确定您是否在将其粘贴到 stackoverflow 时打错了字,但这样的事情会更安全:

sessionFactory
  .getCurrentSession()
  .createQuery("select u from UserEntity u where u.username = :username")
  .setParameter("username", username)
  .uniqueResult();
于 2013-05-02T23:25:51.803 回答