1 22 package org.jboss.ejb3.test.factory; 23 24 import org.jboss.annotation.IgnoreDependency; 25 26 import javax.ejb.EJB ; 27 import javax.ejb.Remote ; 28 import javax.ejb.Stateless ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 import javax.persistence.EntityManager; 32 import javax.persistence.EntityManagerFactory; 33 import javax.persistence.PersistenceContext; 34 import javax.persistence.PersistenceUnit; 35 36 42 @Stateless 43 @Remote (Session2.class) 44 public class Session2Bean implements Session2 45 { 46 @PersistenceContext(unitName="manager2") EntityManager manager2; 47 @PersistenceContext(unitName="../session1.jar#manager1") EntityManager manager1; 48 @PersistenceUnit(unitName="manager2") EntityManagerFactory factory2; 49 @PersistenceUnit(unitName="../session1.jar#manager1") EntityManagerFactory factory1; 50 @IgnoreDependency @EJB Session1 session1; 51 52 54 public Entity1 find1FromManager(int id) 55 { 56 EntityManager m1 = null; 57 try 58 { 59 InitialContext ctx = new InitialContext (); 60 m1 = (EntityManager)ctx.lookup("java:/Manager1"); 61 } 62 catch (NamingException e) 63 { 64 throw new RuntimeException (e); 65 } 66 if (m1.find(Entity1.class, id) == null) throw new RuntimeException ("failed to find"); 67 return manager1.find(Entity1.class, id); 68 } 69 public void find1FromFactory(int id) 70 { 71 EntityManagerFactory f1 = null; 72 try 73 { 74 InitialContext ctx = new InitialContext (); 75 f1 = (EntityManagerFactory)ctx.lookup("java:/Manager1Factory"); 76 } 77 catch (NamingException e) 78 { 79 throw new RuntimeException (e); 80 } 81 EntityManager m1 = f1.createEntityManager(); 82 try 83 { 84 if (m1.find(Entity1.class, id) == null) throw new RuntimeException ("failed to find"); 85 } 86 finally 87 { 88 m1.close(); 89 } 90 91 EntityManager m = factory1.createEntityManager(); 92 Entity1 one = m.find(Entity1.class, id); 93 try 94 { 95 if (one == null) throw new RuntimeException ("NOT FOUND"); 96 } 97 finally 98 { 99 m.close(); 100 } 101 102 } 103 public Entity2 find2FromManager(int id) 104 { 105 return manager2.find(Entity2.class, id); 106 } 107 public void find2FromFactory(int id) 108 { 109 EntityManager m = factory2.createEntityManager(); 110 Entity2 two = m.find(Entity2.class, id); 111 if (two == null) throw new RuntimeException ("NOT FOUND"); 112 } 114 115 public Util findUtil1FromManager(int id) 116 { 117 return manager1.find(Util.class, id); 118 } 119 120 public Util findUtil2FromManager(int id) 121 { 122 return manager2.find(Util.class, id); 123 } 124 } 125 | Popular Tags |