I want to make an authorization in Spring using hibernate, It turns out the error Caused by: org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User where username =?

Classes created

@Repository public class UserDaoImpl implements UserDao { @Autowired private SessionFactory sessionFactory; @SuppressWarnings("unchecked") public User findByUserName(String username) { List<User> users = new ArrayList<User>(); users = (List<User>) sessionFactory.getCurrentSession() .createQuery("from User where username=?") .setParameter(0, username) .list(); System.out.println(users.size()); if (users.size() > 0) { return users.get(0); } else { return null; } } } 

MyUserDetailService

 @Component @Transactional public class MyUserDetailsService implements UserDetailsService { @Autowired UserDao userDao; @Override public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException, DataAccessException { return userDao.findByUserName(username); } } 

In the data.xml prescribed bin

 <bean id="myUserDetailsService" class="kz.tanikin.springtest.service.MyUserDetailsService" /> 

and in security.xml

 <authentication-manager> <authentication-provider user-service-ref='myUserDetailsService'/> </authentication-manager> 

User using standard import org.springframework.security.core.userdetails.User;

What can be wrong?

created two tables users and authority.

    2 answers 2

    If everything is configured correctly, then I would recommend using the Criteria API. It will be something like.

     List<User> users = new ArrayList<User>(); users = (List<User>) sessionFactory.getCurrentSession() .createCriteria(User.class) .add(Restrictions.eq("username",username)) .list(); 

    UPD if User is an object from external lib, then mapping will not work. Then I would recommend to try sql query, it should turn out something like

     List<User> users = new ArrayList<User>(); users = (List<User>) sessionFactory.getCurrentSession() .createSqlQuery("select * from User where username=?") .setParameter(0, username) .addEntity(User.class) .list(); 

    Instead of select * it is better to write select username ... , i.e. specific set of columns in accordance with the properties of the User object.

    UPD2 But the correct option would still be to create your User class with a description of meppings and other things, and then convert it to a Spring User new User(user.getUsername(), user.getPassword(), ...

    • The first two methods do not work, even there are no such meta, createCriteria, and createSqlQuery - J Mas
    • Something is strange. Now there are no projects at hand to accurately check again. But nevertheless, in any case, I highly recommend creating your own entity (class User), which will be mapped on the database table. And then on its basis to create an object class User (spring). - Aries

    The error clearly follows from the Exception message: you forgot to write Hibernate that there is such a User entity — you can do this through the annotatedClasses property. In principle, Hibernate will try to mute the implementation of User from Spring Security if you specify it as an entity in the mapping. But you cannot automatically generate a table for this entity.