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.v1;
023:
024: import java.util.List;
025: import javax.ejb.SessionBean;
026: import javax.ejb.SessionContext;
027: import javax.naming.NamingException;
028: import javax.naming.InitialContext;
029:
030: import org.hibernate.HibernateException;
031: import org.hibernate.Session;
032: import org.hibernate.SessionFactory;
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: */
040: public class PersonBean implements SessionBean {
041: private SessionContext context;
042:
043: public void ejbCreate() {
044: }
045:
046: public void ejbActivate() {
047: }
048:
049: public void ejbPassivate() {
050: }
051:
052: public void ejbRemove() {
053: }
054:
055: public void setSessionContext(SessionContext context) {
056: this .context = context;
057: }
058:
059: public void init() throws HibernateException {
060: Configuration cfg = new Configuration().configure();
061: SchemaExport se = new SchemaExport(cfg);
062: se.create(true, true);
063: }
064:
065: public void sessionInit() throws HibernateException {
066: Configuration cfg = new Configuration().configure();
067: SessionFactory sf = cfg.buildSessionFactory();
068: System.out.println("Initialized session: " + sf);
069: }
070:
071: public Person loadUser(long id) throws HibernateException {
072: return loadUser(new Long(id));
073: }
074:
075: public Person loadUser(Long id) throws HibernateException {
076: return (Person) getSession().load(Person.class, id);
077: }
078:
079: public List listPeople() throws HibernateException {
080: return getSession().createQuery("from Person").list();
081: }
082:
083: public Person loadUser(String name) throws HibernateException {
084: return (Person) getSession().createQuery(
085: "from Person as p where p.name = :name").setString(
086: "name", name).uniqueResult();
087: }
088:
089: public Person storeUser(Person user) throws HibernateException {
090: getSession().saveOrUpdate(user);
091: getSession().flush();
092: return user;
093: }
094:
095: private Session getSession() {
096: try {
097: InitialContext ctx = new InitialContext();
098: String sessionName = (String) ctx
099: .lookup("java:/comp/env/HibernateFactory");
100: SessionFactory sf = (SessionFactory) ctx
101: .lookup(sessionName);
102: return sf.getCurrentSession();
103: } catch (HibernateException e) {
104: throw e;
105: } catch (NamingException e) {
106: throw new HibernateException(e);
107: }
108: }
109:
110: }
|