KickJava   Java API By Example, From Geeks To Geeks.

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

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