KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > manytomany > 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.manytomany;
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 import javax.persistence.PersistenceContext;
31
32 /**
33  * Comment
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 37459 $
37  */

38 @Stateless JavaDoc
39 @Remote JavaDoc(EntityTest.class)
40 public class EntityTestBean implements EntityTest
41 {
42    private @PersistenceContext EntityManager manager;
43
44    public Customer oneToManyCreate() throws Exception JavaDoc
45    {
46       Ticket t = new Ticket();
47       //t.setId( new Long(1) );
48
t.setNumber("33A");
49       Customer c = new Customer();
50       //c.setId( new Long(1) );
51
Set JavaDoc<Ticket> tickets = new HashSet JavaDoc<Ticket>();
52       tickets.add(t);
53       t.setCustomer(c);
54       c.setTickets(tickets);
55       Address address = new Address();
56       address.setStreet("Clarendon Street");
57       address.setCity("Boston");
58       address.setState("MA");
59       address.setZip("02116");
60       c.setAddress(address);
61       manager.persist(c);
62       return c;
63    }
64
65    public Customer findCustomerById(Long JavaDoc id) throws Exception JavaDoc
66    {
67       return manager.find(Customer.class, id);
68    }
69
70    public Flight manyToOneCreate() throws Exception JavaDoc
71    {
72       Flight firstOne = new Flight();
73       firstOne.setId(new Long JavaDoc(1));
74       firstOne.setName("AF0101");
75       Company frenchOne = new Company();
76       frenchOne.setName("Air France");
77       firstOne.setCompany(frenchOne);
78       manager.persist(firstOne);
79       return firstOne;
80    }
81
82    public void manyToManyCreate() throws Exception JavaDoc
83    {
84
85       Flight firstOne = findFlightById(new Long JavaDoc(1));
86       Flight second = new Flight();
87       second.setId(new Long JavaDoc(2));
88       second.setName("US1");
89       Company us = new Company();
90       us.setName("USAir");
91       second.setCompany(us);
92
93       Set JavaDoc<Customer> customers1 = new HashSet JavaDoc<Customer>();
94       Set JavaDoc<Customer> customers2 = new HashSet JavaDoc<Customer>();
95
96
97       Customer bill = new Customer();
98       bill.setName("Bill");
99       customers1.add(bill);
100
101       Customer monica = new Customer();
102       monica.setName("Monica");
103       customers1.add(monica);
104
105       Customer molly = new Customer();
106       molly.setName("Molly");
107       customers2.add(molly);
108
109       firstOne.setCustomers(customers1);
110       second.setCustomers(customers2);
111
112       manager.persist(second);
113    }
114
115
116    public Flight findFlightById(Long JavaDoc id) throws Exception JavaDoc
117    {
118       return manager.find(Flight.class, id);
119    }
120
121    public Company findCompanyById(Integer JavaDoc id) throws Exception JavaDoc
122    {
123       return manager.find(Company.class, id);
124    }
125
126 }
127
Popular Tags