KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > factoryxml > Session2Bean


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.factoryxml;
23
24 import javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.persistence.EntityManager;
27 import javax.persistence.EntityManagerFactory;
28 import javax.persistence.PersistenceContext;
29 import javax.persistence.PersistenceUnit;
30
31 /**
32  * Comment
33  *
34  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
35  * @version $Revision: 42263 $
36  */

37 public class Session2Bean implements Session2
38 {
39    EntityManager manager2;
40    @PersistenceContext(name="manager1") EntityManager manager1;
41    EntityManagerFactory factory2;
42    @PersistenceUnit(name="factory1") EntityManagerFactory factory1;
43    Session1 session1;
44
45    //@Resource SessionContext ctx;
46

47    public Entity1 find1FromManager(int id)
48    {
49       EntityManager m1 = null;
50       try
51       {
52          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
53          m1 = (EntityManager)ctx.lookup("java:/Manager1");
54       }
55       catch (NamingException JavaDoc e)
56       {
57          throw new RuntimeException JavaDoc(e);
58       }
59       if (m1.find(Entity1.class, id) == null) throw new RuntimeException JavaDoc("failed to find");
60       return manager1.find(Entity1.class, id);
61    }
62    public void find1FromFactory(int id)
63    {
64       EntityManagerFactory f1 = null;
65       try
66       {
67          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
68          f1 = (EntityManagerFactory)ctx.lookup("java:/Manager1Factory");
69       }
70       catch (NamingException JavaDoc e)
71       {
72          throw new RuntimeException JavaDoc(e);
73       }
74       EntityManager m1 = f1.createEntityManager();
75       try
76       {
77          if (m1.find(Entity1.class, id) == null) throw new RuntimeException JavaDoc("failed to find");
78       }
79       finally
80       {
81          m1.close();
82       }
83
84       EntityManager m = factory1.createEntityManager();
85       Entity1 one = m.find(Entity1.class, id);
86       try
87       {
88          if (one == null) throw new RuntimeException JavaDoc("NOT FOUND");
89       }
90       finally
91       {
92          m.close();
93       }
94
95    }
96    public Entity2 find2FromManager(int id)
97    {
98       return manager2.find(Entity2.class, id);
99    }
100    public void find2FromFactory(int id)
101    {
102       EntityManager m = factory2.createEntityManager();
103       Entity2 two = m.find(Entity2.class, id);
104       if (two == null) throw new RuntimeException JavaDoc("NOT FOUND");
105       //m.close();
106
}
107
108    public Util findUtil1FromManager(int id)
109    {
110       return manager1.find(Util.class, id);
111    }
112
113    public Util findUtil2FromManager(int id)
114    {
115       return manager2.find(Util.class, id);
116    }
117 }
118
Popular Tags