1 22 package org.jboss.ejb3.test.factoryxml; 23 24 import javax.naming.InitialContext ; 25 import javax.naming.NamingException ; 26 import javax.persistence.EntityManager; 27 import javax.persistence.EntityManagerFactory; 28 import javax.persistence.PersistenceContext; 29 import javax.persistence.PersistenceUnit; 30 31 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 47 public Entity1 find1FromManager(int id) 48 { 49 EntityManager m1 = null; 50 try 51 { 52 InitialContext ctx = new InitialContext (); 53 m1 = (EntityManager)ctx.lookup("java:/Manager1"); 54 } 55 catch (NamingException e) 56 { 57 throw new RuntimeException (e); 58 } 59 if (m1.find(Entity1.class, id) == null) throw new RuntimeException ("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 ctx = new InitialContext (); 68 f1 = (EntityManagerFactory)ctx.lookup("java:/Manager1Factory"); 69 } 70 catch (NamingException e) 71 { 72 throw new RuntimeException (e); 73 } 74 EntityManager m1 = f1.createEntityManager(); 75 try 76 { 77 if (m1.find(Entity1.class, id) == null) throw new RuntimeException ("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 ("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 ("NOT FOUND"); 105 } 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 |