1 25 package org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife; 26 27 import static org.testng.Assert.assertTrue; 28 29 import javax.annotation.PostConstruct; 30 import javax.annotation.Resource; 31 import javax.ejb.EJB ; 32 import javax.ejb.SessionContext ; 33 import javax.persistence.EntityManager; 34 import javax.persistence.PersistenceContext; 35 import javax.persistence.PersistenceContextType; 36 37 import org.objectweb.easybeans.tests.common.ejbs.entity.ebstore.EBStore; 38 39 44 @EJB (name="ejb/ebFactory", beanInterface=ItfPCtxLifetime00.class, mappedName="SFSBPCtxLifeCME01") 45 public class BaseEntityFactory00 implements ItfEntityFactory { 46 47 50 @PersistenceContext(unitName = "testEntity00", type = PersistenceContextType.EXTENDED) 51 private EntityManager em; 52 53 56 @Resource 57 private SessionContext ctx; 58 59 62 private EBStore eb; 63 64 67 private int id = this.hashCode(); 68 69 72 private ItfPCtxLifetime00 bean; 73 74 77 @PostConstruct 78 public void init(){ 79 bean = (ItfPCtxLifetime00) ctx.lookup("ejb/ebFactory"); 80 } 81 82 85 public void createEntity() { 86 eb = new EBStore(id); 87 em.persist(eb); 88 89 bean.createCheckEntity00(); 90 91 this.checkManaged(); 92 bean.checkManaged(); 93 } 94 95 98 @SuppressWarnings ("boxing") 99 public void checkManaged() { 100 assertTrue(em.contains(eb), "The entity instance should be managed."); 101 102 EBStore ebFind = em.find(EBStore.class, id); 103 104 assertTrue(ebFind == eb, "The entity instances should be equals, " 105 + "they are in the same persistence context."); 106 107 bean.checkManaged(); 108 } 109 110 113 @SuppressWarnings ("boxing") 114 public void removeEntity() { 115 EBStore ebFind = em.find(EBStore.class, id); 116 if (ebFind != null) { 117 em.remove(ebFind); 118 } 119 120 ItfPCtxLifetime00 beanRemove = (ItfPCtxLifetime00) ctx.lookup("ejb/ebFactory"); 121 beanRemove.removeEntity(); 122 } 123 } 124 | Popular Tags |