KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > composite > 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.composite;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.ejb.Remote JavaDoc;
27 import javax.ejb.Stateless JavaDoc;
28 import javax.persistence.EntityManager;
29 import javax.persistence.PersistenceContext;
30
31 /**
32  * Comment
33  *
34  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
35  * @version $Revision: 37459 $
36  */

37 @Stateless JavaDoc
38 @Remote JavaDoc(EntityTest.class)
39 public class EntityTestBean implements EntityTest
40 {
41    private @PersistenceContext EntityManager manager;
42    private static long genid = 0;
43
44    public Customer oneToManyCreate(String JavaDoc name) throws Exception JavaDoc
45    {
46       Ticket t = new Ticket();
47       t.setNumber("33A");
48       Customer c = new Customer();
49       CustomerPK pk = new CustomerPK(genid++, name);
50       c.setPk(pk);
51       Set JavaDoc<Ticket> tickets = new HashSet JavaDoc<Ticket>();
52       tickets.add(t);
53       t.setCustomer(c);
54       c.setTickets(tickets);
55       manager.persist(c);
56       return c;
57    }
58
59    public Customer findCustomerByPk(CustomerPK pk) throws Exception JavaDoc
60    {
61       return manager.find(Customer.class, pk);
62    }
63
64    public Flight manyToOneCreate() throws Exception JavaDoc
65    {
66       Flight firstOne = new Flight();
67       firstOne.setId(new Long JavaDoc(1));
68       firstOne.setName("AF0101");
69       manager.persist(firstOne);
70       return firstOne;
71    }
72
73    public void manyToManyCreate() throws Exception JavaDoc
74    {
75
76       Flight firstOne = findFlightById(new Long JavaDoc(1));
77       Flight second = new Flight();
78       second.setId(new Long JavaDoc(2));
79       second.setName("US1");
80
81       Set JavaDoc<Customer> customers1 = new HashSet JavaDoc<Customer>();
82       Set JavaDoc<Customer> customers2 = new HashSet JavaDoc<Customer>();
83
84
85       Customer will = new Customer();
86       CustomerPK willPk = new CustomerPK(genid++, "Will");
87       will.setPk(willPk);
88       customers1.add(will);
89
90       Customer monica = new Customer();
91       CustomerPK moPK = new CustomerPK(genid++, "Monica");
92       monica.setPk(moPK);
93       customers1.add(monica);
94
95       Customer molly = new Customer();
96       CustomerPK mollyPK = new CustomerPK(genid++, "Molly");
97       molly.setPk(mollyPK);
98       customers2.add(molly);
99
100       firstOne.setCustomers(customers1);
101       second.setCustomers(customers2);
102
103       manager.persist(second);
104    }
105
106
107    public Flight findFlightById(Long JavaDoc id) throws Exception JavaDoc
108    {
109       return manager.find(Flight.class, id);
110    }
111
112
113    public FieldCustomer fieldOneToManyCreate(String JavaDoc name) throws Exception JavaDoc
114    {
115       FieldTicket t = new FieldTicket();
116       t.setNumber("33A");
117       FieldCustomer c = new FieldCustomer();
118       FieldCustomerPK pk = new FieldCustomerPK(genid++, name);
119       c.setPk(pk);
120       Set JavaDoc<FieldTicket> tickets = new HashSet JavaDoc<FieldTicket>();
121       tickets.add(t);
122       t.setCustomer(c);
123       c.setTickets(tickets);
124       manager.persist(c);
125       return c;
126    }
127
128    public FieldCustomer fieldFindCustomerByPk(FieldCustomerPK pk) throws Exception JavaDoc
129    {
130       return manager.find(FieldCustomer.class, pk);
131    }
132
133    public FieldFlight fieldManyToOneCreate() throws Exception JavaDoc
134    {
135       FieldFlight firstOne = new FieldFlight();
136       firstOne.setId(new Long JavaDoc(1));
137       firstOne.setName("AF0101");
138       manager.persist(firstOne);
139       return firstOne;
140    }
141
142    public void fieldManyToManyCreate() throws Exception JavaDoc
143    {
144
145       FieldFlight firstOne = fieldFindFlightById(new Long JavaDoc(1));
146       FieldFlight second = new FieldFlight();
147       second.setId(new Long JavaDoc(2));
148       second.setName("US1");
149
150       Set JavaDoc<FieldCustomer> customers1 = new HashSet JavaDoc<FieldCustomer>();
151       Set JavaDoc<FieldCustomer> customers2 = new HashSet JavaDoc<FieldCustomer>();
152
153
154       FieldCustomer will = new FieldCustomer();
155       FieldCustomerPK willPk = new FieldCustomerPK(genid++, "Will");
156       will.setPk(willPk);
157       customers1.add(will);
158
159       FieldCustomer monica = new FieldCustomer();
160       FieldCustomerPK moPK = new FieldCustomerPK(genid++, "Monica");
161       monica.setPk(moPK);
162       customers1.add(monica);
163
164       FieldCustomer molly = new FieldCustomer();
165       FieldCustomerPK mollyPK = new FieldCustomerPK(genid++, "Molly");
166       molly.setPk(mollyPK);
167       customers2.add(molly);
168
169       firstOne.setCustomers(customers1);
170       second.setCustomers(customers2);
171
172       manager.persist(second);
173    }
174
175
176    public FieldFlight fieldFindFlightById(Long JavaDoc id) throws Exception JavaDoc
177    {
178       return manager.find(FieldFlight.class, id);
179    }
180
181
182 }
183
Popular Tags