KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > base > persistencectxlife > BaseEntityFactory00


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BaseEntityFactory00.java 965 2006-07-27 16:27:08Z studzine $
23  * --------------------------------------------------------------------------
24  */

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 JavaDoc;
32 import javax.ejb.SessionContext JavaDoc;
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 /**
40  * This bean creates an entity instance and calls another entity factory to create another entity.
41  * @author Eduardo Studzinski Estima de Castro
42  * @author Gisele Pinheiro Souza
43  */

44 @EJB JavaDoc(name="ejb/ebFactory", beanInterface=ItfPCtxLifetime00.class, mappedName="SFSBPCtxLifeCME01")
45 public class BaseEntityFactory00 implements ItfEntityFactory {
46
47     /**
48      * Manager.
49      */

50     @PersistenceContext(unitName = "testEntity00", type = PersistenceContextType.EXTENDED)
51     private EntityManager em;
52
53     /**
54      * Context.
55      */

56     @Resource
57     private SessionContext JavaDoc ctx;
58
59     /**
60      * Entity.
61      */

62     private EBStore eb;
63
64     /**
65      * Id.
66      */

67     private int id = this.hashCode();
68
69     /**
70      * Entity Creator.
71      */

72     private ItfPCtxLifetime00 bean;
73
74     /**
75      * Initialization.
76      */

77     @PostConstruct
78     public void init(){
79         bean = (ItfPCtxLifetime00) ctx.lookup("ejb/ebFactory");
80     }
81
82     /**
83      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfEntityFactory
84      */

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     /**
96      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfEntityFactory
97      */

98     @SuppressWarnings JavaDoc("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     /**
111      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfEntityFactory
112      */

113     @SuppressWarnings JavaDoc("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