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


}

    }

ユーザーの詳細を保持する 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 {}.
4

1 に答える 1