KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > hibernate > model > v2 > PersonBean


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
23 package org.jboss.test.hibernate.model.v2;
24
25 import java.util.List JavaDoc;
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.ejb.SessionContext JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import org.hibernate.HibernateException;
32 import org.hibernate.SessionFactory;
33 import org.hibernate.Session;
34 import org.hibernate.tool.hbm2ddl.SchemaExport;
35 import org.hibernate.cfg.Configuration;
36
37 /**
38  @author Scott.Stark@jboss.org
39  @version $Revision: 41308 $ */

40 public class PersonBean implements SessionBean JavaDoc
41 {
42    private SessionContext JavaDoc context;
43
44    public void ejbCreate()
45    {
46    }
47
48    public void ejbActivate()
49    {
50    }
51
52    public void ejbPassivate()
53    {
54    }
55
56    public void ejbRemove()
57    {
58    }
59
60    public void setSessionContext(SessionContext JavaDoc context)
61    {
62       this.context = context;
63    }
64
65    public void init( )
66       throws HibernateException
67    {
68       Configuration cfg = new Configuration().configure();
69       SchemaExport se = new SchemaExport(cfg);
70       se.create(true, true);
71    }
72    public void sessionInit( )
73       throws HibernateException
74    {
75       Configuration cfg = new Configuration().configure();
76       SessionFactory sf = cfg.buildSessionFactory();
77       System.out.println("Initialized session: "+sf);
78    }
79
80    public Person loadUser(long id) throws HibernateException
81    {
82       return loadUser( new Long JavaDoc(id) );
83    }
84
85    public Person loadUser(Long JavaDoc id) throws HibernateException
86    {
87       return (Person) getSession().load(Person.class, id);
88    }
89
90    public List JavaDoc listPeople() throws HibernateException
91    {
92       return getSession()
93             .createQuery("from Person")
94             .list();
95    }
96
97    public Person loadUser(String JavaDoc name) throws HibernateException
98    {
99       return (Person) getSession()
100             .createQuery("from Person as p where p.name = :name")
101             .setString("name", name)
102             .uniqueResult();
103    }
104
105    public Person storeUser(Person user) throws HibernateException
106    {
107       getSession().saveOrUpdate(user);
108       getSession().flush();
109       return user;
110    }
111
112    private Session getSession()
113    {
114       try
115       {
116          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
117          String JavaDoc sessionName = (String JavaDoc) ctx.lookup("java:/comp/env/HibernateFactory");
118          SessionFactory sf = (SessionFactory) ctx.lookup(sessionName);
119          return sf.getCurrentSession();
120       }
121       catch( HibernateException e )
122       {
123          throw e;
124       }
125       catch( NamingException JavaDoc e )
126       {
127          throw new HibernateException(e);
128       }
129    }
130
131 }
132
Popular Tags