KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > entity > EntityTestBean


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.entity;
23
24 import org.hibernate.Session;
25 import org.jboss.annotation.JndiInject;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.ejb.Remote JavaDoc;
32 import javax.ejb.Stateless JavaDoc;
33 import javax.ejb.TransactionAttribute JavaDoc;
34 import javax.ejb.TransactionAttributeType JavaDoc;
35 import javax.persistence.EntityManager;
36 import javax.persistence.PersistenceContext;
37 import javax.persistence.Query;
38 import javax.transaction.TransactionManager JavaDoc;
39 import javax.transaction.Transaction JavaDoc;
40 import javax.transaction.SystemException JavaDoc;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45
46 /**
47  * Comment
48  *
49  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
50  * @version $Revision: 42799 $
51  */

52 @Stateless JavaDoc
53 @Remote JavaDoc(EntityTest.class)
54 public class EntityTestBean implements EntityTest
55 {
56    private @PersistenceContext EntityManager manager;
57    private @PersistenceContext Session session;
58    private @JndiInject(jndiName="java:/TransactionManager") TransactionManager JavaDoc tm;
59    private static Log log = LogFactory.getLog( "org.hibernate.ejb" );
60
61
62    @TransactionAttribute JavaDoc(TransactionAttributeType.NOT_SUPPORTED)
63    public void testOutsideTransaction()
64    {
65       Transaction JavaDoc tx = null;
66       try
67       {
68          tx = tm.getTransaction();
69       }
70       catch (SystemException JavaDoc e)
71       {
72          throw new RuntimeException JavaDoc(e);
73       }
74       if (tx != null) throw new RuntimeException JavaDoc("THERE IS A TRANSACTION!!!");
75       Query q = manager.createQuery("SELECT c FROM Customer c");
76       List JavaDoc l = q.getResultList();
77       if (l.size() == 0) throw new RuntimeException JavaDoc("failed");
78       org.hibernate.Query q2 = session.createQuery("FROM Customer c");
79       l = q2.list();
80       if (l.size() == 0) throw new RuntimeException JavaDoc("failed");
81
82    }
83
84    public Customer oneToManyCreate() throws Exception JavaDoc
85    {
86       Ticket t = new Ticket();
87       //t.setId( new Long(1) );
88
t.setNumber("33A");
89       Customer c = new Customer();
90       //c.setId( new Long(1) );
91
Set JavaDoc<Ticket> tickets = new HashSet JavaDoc<Ticket>();
92       tickets.add(t);
93       t.setCustomer(c);
94       c.setTickets(tickets);
95       Address address = new Address();
96       address.setStreet("Clarendon Street");
97       address.setCity("Boston");
98       address.setState("MA");
99       address.setZip("02116");
100       c.setAddress(address);
101       manager.persist(c);
102       return c;
103    }
104
105    public Customer findCustomerById(Long JavaDoc id) throws Exception JavaDoc
106    {
107       return manager.find(Customer.class, id);
108    }
109
110    public Flight manyToOneCreate() throws Exception JavaDoc
111    {
112       Flight firstOne = new Flight();
113       firstOne.setId(new Long JavaDoc(1));
114       firstOne.setName("AF0101");
115       Company frenchOne = new Company();
116       frenchOne.setName("Air France");
117       firstOne.setCompany(frenchOne);
118       manager.persist(firstOne);
119       return firstOne;
120    }
121
122    public void manyToManyCreate() throws Exception JavaDoc
123    {
124
125       Flight firstOne = findFlightById(new Long JavaDoc(1));
126       Flight second = new Flight();
127       second.setId(new Long JavaDoc(2));
128       second.setName("US1");
129       Company us = new Company();
130       us.setName("USAir");
131       second.setCompany(us);
132
133       Set JavaDoc<Customer> customers1 = new HashSet JavaDoc<Customer>();
134       Set JavaDoc<Customer> customers2 = new HashSet JavaDoc<Customer>();
135
136
137       Customer bill = new Customer();
138       bill.setName("Bill");
139       customers1.add(bill);
140
141       Customer monica = new Customer();
142       monica.setName("Monica");
143       customers1.add(monica);
144
145       Customer molly = new Customer();
146       molly.setName("Molly");
147       customers2.add(molly);
148
149       firstOne.setCustomers(customers1);
150       second.setCustomers(customers2);
151
152       manager.persist(second);
153    }
154
155
156    public Flight findFlightById(Long JavaDoc id) throws Exception JavaDoc
157    {
158       return manager.find(Flight.class, id);
159    }
160
161    public Company findCompanyById(Integer JavaDoc id) throws Exception JavaDoc
162    {
163       return manager.find(Company.class, id);
164    }
165
166    public FieldCustomer fieldOneToManyCreate() throws Exception JavaDoc
167    {
168       FieldTicket t = new FieldTicket();
169       //t.setId( new Long(1) );
170
t.setNumber("33A");
171       FieldCustomer c = new FieldCustomer();
172       //c.setId( new Long(1) );
173
Set JavaDoc<FieldTicket> tickets = new HashSet JavaDoc<FieldTicket>();
174       tickets.add(t);
175       t.setCustomer(c);
176       c.setTickets(tickets);
177       FieldAddress address = new FieldAddress();
178       address.setStreet("Clarendon Street");
179       address.setCity("Boston");
180       address.setState("MA");
181       address.setZip("02116");
182       c.setAddress(address);
183       manager.persist(c);
184       return c;
185    }
186
187    public FieldCustomer fieldFindCustomerById(Long JavaDoc id) throws Exception JavaDoc
188    {
189       return manager.find(FieldCustomer.class, id);
190    }
191
192    public FieldFlight fieldManyToOneCreate() throws Exception JavaDoc
193    {
194       FieldFlight firstOne = new FieldFlight();
195       firstOne.setId(new Long JavaDoc(1));
196       firstOne.setName("AF0101");
197       FieldCompany frenchOne = new FieldCompany();
198       frenchOne.setName("Air France");
199       firstOne.setCompany(frenchOne);
200       manager.persist(firstOne);
201       return firstOne;
202    }
203
204    public void fieldManyToManyCreate() throws Exception JavaDoc
205    {
206
207       FieldFlight firstOne = fieldFindFlightById(new Long JavaDoc(1));
208       FieldFlight second = new FieldFlight();
209       second.setId(new Long JavaDoc(2));
210       second.setName("US1");
211       FieldCompany us = new FieldCompany();
212       us.setName("USAir");
213       second.setCompany(us);
214
215       Set JavaDoc<FieldCustomer> customers1 = new HashSet JavaDoc<FieldCustomer>();
216       Set JavaDoc<FieldCustomer> customers2 = new HashSet JavaDoc<FieldCustomer>();
217
218
219       FieldCustomer bill = new FieldCustomer();
220       bill.setName("Bill");
221       customers1.add(bill);
222
223       FieldCustomer monica = new FieldCustomer();
224       monica.setName("Monica");
225       customers1.add(monica);
226
227       FieldCustomer molly = new FieldCustomer();
228       molly.setName("Molly");
229       customers2.add(molly);
230
231       firstOne.setCustomers(customers1);
232       second.setCustomers(customers2);
233
234       manager.persist(second);
235    }
236
237
238    public FieldFlight fieldFindFlightById(Long JavaDoc id) throws Exception JavaDoc
239    {
240       return manager.find(FieldFlight.class, id);
241    }
242
243    public FieldCompany fieldFindCompanyById(Integer JavaDoc id) throws Exception JavaDoc
244    {
245       return manager.find(FieldCompany.class, id);
246    }
247
248    public void testNamedQueries() throws Exception JavaDoc
249    {
250       System.out.println("testNamedQueries()");
251       ArrayList JavaDoc ids = new ArrayList JavaDoc();
252       Airport ap1 = new Airport("OSL", "Oslo");
253       manager.persist(ap1);
254
255       Airport ap2 = new Airport("LHR", "London");
256       manager.persist(ap2);
257
258       Airport ap3 = new Airport("LAX", "Los Angeles");
259       manager.persist(ap3);
260
261       List JavaDoc list = manager.createNamedQuery("allAirports").getResultList();
262       if (list.size() != 3) throw new RuntimeException JavaDoc("Wrong number returned for allAirports query " + list.size());
263
264       Airport ap = (Airport)manager.createNamedQuery("airportById").setParameter("id", ap2.getId()).getSingleResult();
265       if (ap == null) throw new RuntimeException JavaDoc("No object returned by airportById query");
266
267       FieldAirport fap1 = new FieldAirport("LGW", "London");
268       manager.persist(fap1);
269
270       FieldAirport fap2 = new FieldAirport("ORL", "Paris");
271       manager.persist(fap2);
272
273       FieldAirport fap = (FieldAirport)manager.createNamedQuery("airportByCode").setParameter("code", "LGW").getSingleResult();
274       if (fap == null) throw new RuntimeException JavaDoc("No object returned by airportById query");
275    }
276    
277    public Customer createCustomer(String JavaDoc name) {
278        Customer c = new Customer();
279        c.setName(name);
280        manager.persist(c);
281        return c;
282    }
283    
284    public void changeCustomer(Long JavaDoc id, String JavaDoc name) {
285        Customer c = manager.find(Customer.class, id);
286        c.setName(name);
287    }
288    
289    public Customer loadCustomer(Long JavaDoc id) {
290        Customer c = manager.find(Customer.class, id);
291        return c;
292    }
293 }
294
Popular Tags