Reputation: 5313
I am using spring-security on top of a Spring-MVC application. Currently I am working on a project where I want to authenticate a user. I have already implemented userDetailsService, UserDetails. Now when I get the username from the HTML form, I am passing the same to the DAO, where I can findByUsername. As username is not the primary key, I am using a Hibernate query to extract the user based upon username. I am getting the following error, kindly have a look. Thank you.
Error code : (akshay is username)
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));
}
User :
@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);
}
}
Any pointers would be nice. I tried multiple ways of queries, mentioned in the documentation, none of them works and I get the same error. Thank you.
Upvotes: 1
Views: 657
Reputation: 34816
LoginServiceImpl
calls the UserDao
, but obviously no Hibernate session is created for the current thread when that happens.
Try adding @Transactional
annotation on the UserDetailsService
interface or you can add the @Transactional
annotation even on the UserDao#findByName
method itself. That way a Hibernate session should be present when the DAO method is called.
I'm assuming that you have transaction manager set-up in your Spring configuration.
Upvotes: 3