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.ejb3.test.factory;
023:
024: import javax.ejb.Remote;
025: import javax.ejb.SessionContext;
026: import javax.ejb.Stateless;
027: import javax.ejb.EJB;
028: import javax.persistence.EntityManager;
029: import javax.persistence.EntityManagerFactory;
030: import javax.persistence.PersistenceContext;
031: import javax.persistence.PersistenceUnit;
032: import javax.annotation.Resource;
033:
034: import org.hibernate.SessionFactory;
035: import org.hibernate.Session;
036:
037: /**
038: * Comment
039: *
040: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
041: * @version $Revision: 57207 $
042: */
043: @Stateless
044: @Remote(Session1.class)
045: @PersistenceContext(name="encManager1")
046: public class Session1Bean implements Session1 {
047: @PersistenceContext(unitName="manager1")
048: Session manager1;
049: @PersistenceContext(unitName="../session2.jar#manager2")
050: EntityManager manager2;
051: @PersistenceUnit(unitName="manager1")
052: SessionFactory factory1;
053: @PersistenceUnit(unitName="../session2.jar#manager2")
054: EntityManagerFactory factory2;
055: @EJB
056: Session2 session2;
057:
058: @Resource
059: SessionContext ctx;
060:
061: public int create1FromManager() {
062: EntityManager enc = (EntityManager) ctx.lookup("encManager1");
063: Entity1 first = new Entity1();
064: first.setString("blah");
065: enc.persist(first);
066:
067: Entity1 one = new Entity1();
068: one.setString("oneManager");
069: manager1.save(one);
070: return one.getId();
071: }
072:
073: public int create1FromFactory() {
074: Entity1 one = new Entity1();
075: Session m = factory1.openSession();
076: m.save(one);
077: // leak m.close();
078: return one.getId();
079: }
080:
081: public int create2FromManager() {
082: Entity2 two = new Entity2();
083: two.setString("twoManager");
084: manager2.persist(two);
085: return two.getId();
086: }
087:
088: public int create2FromFactory() {
089: Entity2 two = new Entity2();
090: EntityManager m = factory2.createEntityManager();
091: m.persist(two);
092: // leak m.close();
093: return two.getId();
094: }
095:
096: public void doUtil(Util ux) {
097: Util u = new Util();
098: u.setId(1);
099: u.setName("one");
100: manager1.persist(u);
101:
102: u = new Util();
103: u.setId(2);
104: u.setName("two");
105: manager2.persist(u);
106: }
107:
108: }
|