View Javadoc

1   /***
2    * 
3    */
4   package hu.elte.tribus.services;
5   
6   import java.sql.SQLException;
7   
8   import org.hibernate.Criteria;
9   import org.hibernate.HibernateException;
10  import org.hibernate.Session;
11  import org.hibernate.criterion.Restrictions;
12  import org.springframework.orm.hibernate3.HibernateCallback;
13  import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
14  
15  import hu.elte.tribus.interfaces.UserDao;
16  import hu.elte.tribus.model.User;
17  
18  /***
19   * @author kocka
20   * 
21   */
22  public class HibernateUserDao extends HibernateDaoSupport implements UserDao {
23  
24  	public User getUser(final String name) {
25  		return (User) getHibernateTemplate().execute(new HibernateCallback() {
26  
27  			public Object doInHibernate(Session session)
28  					throws HibernateException, SQLException {
29  				Criteria criteria = session.createCriteria(User.class);
30  				criteria.add(Restrictions.eq("username", name));
31  				User user = (User) criteria.uniqueResult();
32  				return user;
33  			}
34  		});
35  	}
36  
37  	public void saveUser(User user) {
38  		getHibernateTemplate().save(user);
39  	}
40  
41  	public User getUserById(Integer id) {
42  		return (User) getHibernateTemplate().load(User.class, id);
43  	}
44  
45  }