KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > relationships > SessionBean


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.relationships;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator 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(SessionRemote.class)
40 public class SessionBean implements SessionRemote
41 {
42    @PersistenceContext
43    private EntityManager em;
44
45    public void createCategory()
46    {
47       Category category = new Category();
48       category.setId(1);
49       em.persist(category);
50    }
51
52    public void createItem()
53    {
54       System.out.println("++++++++++++++++++++++");
55       Category category = em.find(Category.class, 1L);
56       Item item = new Item();
57       item.setId(1);
58       item.setName("hello");
59       category.getItems().add(item);
60    }
61
62    public int getNumItems()
63    {
64       Category category = em.find(Category.class, 1L);
65       for (Item i : category.getItems())
66       {
67          System.out.println(i.getName());
68       }
69       return category.getItems().size();
70    }
71
72    public long createCustomer()
73    {
74       Customer cust = new Customer();
75       CustomerRecord record = new CustomerRecord();
76       cust.setCustomerRecord(record);
77       //record.setCustomer(cust);
78
em.persist(cust);
79       return cust.getId();
80    }
81
82    public void testInverse(long id)
83    {
84       CustomerRecord record = em.find(CustomerRecord.class, id);
85       record.setCustomer(null);
86    }
87
88
89    public Customer getCustomer(long id)
90    {
91       return em.find(Customer.class, id);
92    }
93
94    public long creatOrder()
95    {
96       Order order = new Order();
97       em.persist(order);
98       LineItem beer = new LineItem();
99       beer.setProduct("beer");
100       LineItem wine = new LineItem();
101       wine.setProduct("wine");
102
103       beer.setOrder(order);
104       em.persist(beer);
105       wine.setOrder(order);
106       em.persist(wine);
107       return order.getId();
108    }
109
110    public Order getOrder(long id)
111    {
112       return em.find(Order.class, id);
113    }
114
115    public void deleteOne(long itemId)
116    {
117       LineItem item = em.find(LineItem.class, itemId);
118       item.getOrder().getItems().remove(item);
119       em.remove(item);
120    }
121
122    public void deleteFromCollection(long orderId)
123    {
124       Order order = em.find(Order.class, orderId);
125       Collection JavaDoc<LineItem> items = order.getItems();
126       Iterator JavaDoc<LineItem> it = items.iterator();
127       while (it.hasNext())
128       {
129          LineItem item = it.next();
130          it.remove();
131          item.setOrder(null);
132          break;
133       }
134       System.out.println("****** " + order.getItems().size());
135    }
136
137    public void merge(Order order)
138    {
139       em.merge(order);
140    }
141 }
142
Popular Tags