001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.hibernate.model.v2;
023:
024: import java.util.List;
025: import javax.ejb.SessionBean;
026: import javax.ejb.SessionContext;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029:
030: import org.hibernate.HibernateException;
031: import org.hibernate.SessionFactory;
032: import org.hibernate.Session;
033: import org.hibernate.tool.hbm2ddl.SchemaExport;
034: import org.hibernate.cfg.Configuration;
035:
036: /**
037: @author Scott.Stark@jboss.org
038: @version $Revision: 57211 $ */
039: public class PersonBean implements SessionBean {
040: private SessionContext context;
041:
042: public void ejbCreate() {
043: }
044:
045: public void ejbActivate() {
046: }
047:
048: public void ejbPassivate() {
049: }
050:
051: public void ejbRemove() {
052: }
053:
054: public void setSessionContext(SessionContext context) {
055: this .context = context;
056: }
057:
058: public void init() throws HibernateException {
059: Configuration cfg = new Configuration().configure();
060: SchemaExport se = new SchemaExport(cfg);
061: se.create(true, true);
062: }
063:
064: public void sessionInit() throws HibernateException {
065: Configuration cfg = new Configuration().configure();
066: SessionFactory sf = cfg.buildSessionFactory();
067: System.out.println("Initialized session: " + sf);
068: }
069:
070: public Person loadUser(long id) throws HibernateException {
071: return loadUser(new Long(id));
072: }
073:
074: public Person loadUser(Long id) throws HibernateException {
075: return (Person) getSession().load(Person.class, id);
076: }
077:
078: public List listPeople() throws HibernateException {
079: return getSession().createQuery("from Person").list();
080: }
081:
082: public Person loadUser(String name) throws HibernateException {
083: return (Person) getSession().createQuery(
084: "from Person as p where p.name = :name").setString(
085: "name", name).uniqueResult();
086: }
087:
088: public Person storeUser(Person user) throws HibernateException {
089: getSession().saveOrUpdate(user);
090: getSession().flush();
091: return user;
092: }
093:
094: private Session getSession() {
095: try {
096: InitialContext ctx = new InitialContext();
097: String sessionName = (String) ctx
098: .lookup("java:/comp/env/HibernateFactory");
099: SessionFactory sf = (SessionFactory) ctx
100: .lookup(sessionName);
101: return sf.getCurrentSession();
102: } catch (HibernateException e) {
103: throw e;
104: } catch (NamingException e) {
105: throw new HibernateException(e);
106: }
107: }
108:
109: }
|