Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.appfuse.dao.hibernate;
 
This class interacts with Spring's HibernateTemplate to save/delete and retrieve User objects.

Author(s):
Matt Raible Modified by Dan Kibler Extended to implement Acegi UserDetailsService interface by David Carter david@carter.net Modified by Bryan Noll to work with the new BaseDaoHibernate implementation that uses generics.
@Repository("userDao")
public class UserDaoHibernate extends GenericDaoHibernate<UserLongimplements UserDaoUserDetailsService {

    
Constructor that sets the entity to User.class.
    public UserDaoHibernate() {
        super(User.class);
    }

    
    @SuppressWarnings("unchecked")
    public List<UsergetUsers() {
        return getHibernateTemplate().find("from User u order by upper(u.username)");
    }

    
    public User saveUser(User user) {
        if (.isDebugEnabled()) {
            .debug("user's id: " + user.getId());
        }
        getHibernateTemplate().saveOrUpdate(user);
        // necessary to throw a DataIntegrityViolation and catch it in UserManager
        getHibernateTemplate().flush();
        return user;
    }

    
Overridden simply to call the saveUser method. This is happenening because saveUser flushes the session and saveObject of BaseDaoHibernate does not.

Parameters:
user the user to save
Returns:
the modified user (with a primary key set if they're new)
    @Override
    public User save(User user) {
        return this.saveUser(user);
    }

    
    public UserDetails loadUserByUsername(String usernamethrows UsernameNotFoundException {
        List users = getHibernateTemplate().find("from User where username=?"username);
        if (users == null || users.isEmpty()) {
            throw new UsernameNotFoundException("user '" + username + "' not found...");
        } else {
            return (UserDetailsusers.get(0);
        }
    }

    
    public String getUserPassword(String username) {
        SimpleJdbcTemplate jdbcTemplate =
                new SimpleJdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
        Table table = AnnotationUtils.findAnnotation(User.classTable.class);
        return jdbcTemplate.queryForObject(
                "select password from " + table.name() + " where username=?"String.classusername);
    }
    
New to GrepCode? Check out our FAQ X