KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > factory > Session1Bean


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 package org.jboss.ejb3.test.factory;
23
24 import javax.ejb.Remote JavaDoc;
25 import javax.ejb.SessionContext JavaDoc;
26 import javax.ejb.Stateless JavaDoc;
27 import javax.ejb.EJB JavaDoc;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.PersistenceContext;
31 import javax.persistence.PersistenceUnit;
32 import javax.annotation.Resource;
33
34 import org.hibernate.SessionFactory;
35 import org.hibernate.Session;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 45473 $
42  */

43 @Stateless JavaDoc
44 @Remote JavaDoc(Session1.class)
45 @PersistenceContext(name="encManager1")
46 public class Session1Bean implements Session1
47 {
48    @PersistenceContext(unitName="manager1") Session manager1;
49    @PersistenceContext(unitName="../session2.jar#manager2") EntityManager manager2;
50    @PersistenceUnit(unitName="manager1") SessionFactory factory1;
51    @PersistenceUnit(unitName="../session2.jar#manager2") EntityManagerFactory factory2;
52    @EJB JavaDoc Session2 session2;
53    
54    @Resource SessionContext JavaDoc ctx;
55
56    public int create1FromManager()
57    {
58       EntityManager enc = (EntityManager)ctx.lookup("encManager1");
59       Entity1 first = new Entity1();
60       first.setString("blah");
61       enc.persist(first);
62       
63       Entity1 one = new Entity1();
64       one.setString("oneManager");
65       manager1.save(one);
66       return one.getId();
67    }
68    public int create1FromFactory()
69    {
70       Entity1 one = new Entity1();
71       Session m = factory1.openSession();
72       m.save(one);
73       // leak m.close();
74
return one.getId();
75    }
76
77    public int create2FromManager()
78    {
79       Entity2 two = new Entity2();
80       two.setString("twoManager");
81       manager2.persist(two);
82       return two.getId();
83    }
84    public int create2FromFactory()
85    {
86       Entity2 two = new Entity2();
87       EntityManager m = factory2.createEntityManager();
88       m.persist(two);
89       // leak m.close();
90
return two.getId();
91    }
92
93    public void doUtil(Util ux)
94    {
95       Util u = new Util();
96       u.setId(1);
97       u.setName("one");
98       manager1.persist(u);
99
100       u = new Util();
101       u.setId(2);
102       u.setName("two");
103       manager2.persist(u);
104    }
105
106 }
107
Popular Tags