KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > hibernate > ProfileService


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
30
31 /**
32  * A service bean used to interact with a bound hibernate session for
33  * testing purposes.
34  *
35  * @author <a HREF="mailto:steve@hibernate.org">Steve Ebersole</a>
36  * @version $Revision: 37406 $
37  */

38 public class ProfileService
39 {
40    private static final String JavaDoc SESSION_FACTORY_NAME = "java:/hibernate/SessionFactory";
41
42    public User loadUser(long id) throws HibernateException
43    {
44       return loadUser( new Long JavaDoc(id) );
45    }
46
47    public User loadUser(Long JavaDoc id) throws HibernateException
48    {
49       return (User) getSession().load(User.class, id);
50    }
51
52    public List JavaDoc listUsers() throws HibernateException
53    {
54       return getSession()
55             .createQuery("from User")
56             .list();
57    }
58
59    public User locateUser(String JavaDoc 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