org.hibernate.HibernateException:在身份验证时找不到当前线程的会话

时间:2014-10-09 13:41:02

标签: hibernate spring-security

我在Spring-MVC应用程序之上使用spring-security。目前我正在开展一个我想要对用户进行身份验证的项目。我已经实现了userDetailsS​​ervice,UserDetails。现在,当我从HTML表单中获取用户名时,我将其传递给DAO,在那里我可以找到ByUsername。由于用户名不是主键,我使用Hibernate查询根据用户名提取用户。我收到以下错误,请好好看看。谢谢。

错误代码:(akshay是用户名)

Caused by: org.postgresql.util.PSQLException: ERROR: column "akshay" does not exist
  Position: 639
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:304)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    ... 65 more

UserDaoImpl:

 @Override
    public User findByName(String username) {
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery("FROM User as u where u.username="+username);
        User p = (User)query.uniqueResult();
        return p;

       // User p = (User)session.get(User.class,new String(username));

    }

用户:

@Entity
@Table(name="registration")
public class User implements UserDetails{
    private static final GrantedAuthority USER_AUTH = new SimpleGrantedAuthority("ROLE_USER");

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "my_entity_seq_gen")
    @SequenceGenerator(name ="my_entity_seq_gen", sequenceName = "MY_ENTITY_SEQ")
    public Long id;

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

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

LoginServiceImple:

@Service("userDetailsService")
public class LoginServiceImpl implements UserDetailsService{
    @Autowired private UserDao userDao;
    @Autowired private Assembler assembler;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException,DataAccessException {
        //    UserDetails userDetails = null;
        System.out.println("LoadingUSER");  // Debug purposes, remove later
        User user = userDao.findByName(username);
            if(user == null) { throw new UsernameNotFoundException("Wrong username or password");} //Never specify which one was it exactly
        return assembler.buildUserFromUserEntity(user);
    }
}

任何指针都会很好。我尝试了多种查询方式,在文档中提到,它们都不起作用,我得到了同样的错误。谢谢。

1 个答案:

答案 0 :(得分:1)

LoginServiceImpl调用UserDao,但显然没有为当前线程创建Hibernate会话。

尝试在@Transactional界面上添加UserDetailsService注释,或者即使在@Transactional方法本身上也可以添加UserDao#findByName注释。这样,当调用DAO方法时,应该存在Hibernate会话。

我假设您在Spring配置中设置了事务管理器。