1 22 package org.jboss.test.hibernate; 23 24 import org.jboss.test.hibernate.model.User; 25 import org.jboss.hibernate.session.HibernateContext; 26 import org.hibernate.HibernateException; 27 import org.hibernate.Session; 28 29 import java.util.List ; 30 31 38 public class ProfileService 39 { 40 private static final String SESSION_FACTORY_NAME = "java:/hibernate/SessionFactory"; 41 42 public User loadUser(long id) throws HibernateException 43 { 44 return loadUser( new Long (id) ); 45 } 46 47 public User loadUser(Long id) throws HibernateException 48 { 49 return (User) getSession().load(User.class, id); 50 } 51 52 public List listUsers() throws HibernateException 53 { 54 return getSession() 55 .createQuery("from User") 56 .list(); 57 } 58 59 public User locateUser(String handle) throws HibernateException 60 { 61 return (User) getSession() 62 .createQuery("from User as u where u.handle = :handle") 63 .setString("handle", handle) 64 .uniqueResult(); 65 } 66 67 public User storeUser(User user) throws HibernateException 68 { 69 getSession().saveOrUpdate(user); 70 getSession().flush(); 71 return user; 72 } 73 74 private Session getSession() 75 { 76 return HibernateContext.getSession(SESSION_FACTORY_NAME); 77 } 78 } 79 | Popular Tags |