001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.test.manytomany;
023:
024: import java.util.HashSet;
025: import java.util.Set;
026: import javax.ejb.Remote;
027: import javax.ejb.Stateless;
028: import javax.persistence.EntityManager;
029: import javax.persistence.PersistenceContext;
030: import javax.persistence.PersistenceContext;
031:
032: /**
033: * Comment
034: *
035: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
036: * @version $Revision: 57207 $
037: */
038: @Stateless
039: @Remote(EntityTest.class)
040: public class EntityTestBean implements EntityTest {
041: private @PersistenceContext
042: EntityManager manager;
043:
044: public Customer oneToManyCreate() throws Exception {
045: Ticket t = new Ticket();
046: //t.setId( new Long(1) );
047: t.setNumber("33A");
048: Customer c = new Customer();
049: //c.setId( new Long(1) );
050: Set<Ticket> tickets = new HashSet<Ticket>();
051: tickets.add(t);
052: t.setCustomer(c);
053: c.setTickets(tickets);
054: Address address = new Address();
055: address.setStreet("Clarendon Street");
056: address.setCity("Boston");
057: address.setState("MA");
058: address.setZip("02116");
059: c.setAddress(address);
060: manager.persist(c);
061: return c;
062: }
063:
064: public Customer findCustomerById(Long id) throws Exception {
065: return manager.find(Customer.class, id);
066: }
067:
068: public Flight manyToOneCreate() throws Exception {
069: Flight firstOne = new Flight();
070: firstOne.setId(new Long(1));
071: firstOne.setName("AF0101");
072: Company frenchOne = new Company();
073: frenchOne.setName("Air France");
074: firstOne.setCompany(frenchOne);
075: manager.persist(firstOne);
076: return firstOne;
077: }
078:
079: public void manyToManyCreate() throws Exception {
080:
081: Flight firstOne = findFlightById(new Long(1));
082: Flight second = new Flight();
083: second.setId(new Long(2));
084: second.setName("US1");
085: Company us = new Company();
086: us.setName("USAir");
087: second.setCompany(us);
088:
089: Set<Customer> customers1 = new HashSet<Customer>();
090: Set<Customer> customers2 = new HashSet<Customer>();
091:
092: Customer bill = new Customer();
093: bill.setName("Bill");
094: customers1.add(bill);
095:
096: Customer monica = new Customer();
097: monica.setName("Monica");
098: customers1.add(monica);
099:
100: Customer molly = new Customer();
101: molly.setName("Molly");
102: customers2.add(molly);
103:
104: firstOne.setCustomers(customers1);
105: second.setCustomers(customers2);
106:
107: manager.persist(second);
108: }
109:
110: public Flight findFlightById(Long id) throws Exception {
111: return manager.find(Flight.class, id);
112: }
113:
114: public Company findCompanyById(Integer id) throws Exception {
115: return manager.find(Company.class, id);
116: }
117:
118: }
|