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:
023: package org.jboss.ejb3.test.clusteredentity;
024:
025: import java.util.HashSet;
026: import java.util.List;
027: import java.util.Set;
028:
029: import javax.ejb.Remote;
030: import javax.ejb.Remove;
031: import javax.ejb.Stateless;
032: import javax.persistence.EntityManager;
033: import javax.persistence.FlushModeType;
034: import javax.persistence.PersistenceContext;
035:
036: /**
037: * @author Brian Stansberry
038: * @version $Revision$
039: */
040: @Stateless
041: @Remote(BulkOperationsTest.class)
042: public class BulkOperationsTestBean implements BulkOperationsTest {
043: @PersistenceContext
044: private EntityManager manager;
045:
046: public void createContacts() {
047: for (int i = 0; i < 10; i++)
048: createCustomer(i);
049: }
050:
051: public int deleteContacts() {
052: String deleteHQL = "delete Contact where customer in ";
053: deleteHQL += " (select customer FROM Customer as customer ";
054: deleteHQL += " where customer.name = :cName)";
055:
056: int rowsAffected = manager.createQuery(deleteHQL).setFlushMode(
057: FlushModeType.AUTO).setParameter("cName", "Red Hat")
058: .executeUpdate();
059: return rowsAffected;
060: }
061:
062: public List<Integer> getContactsByCustomer(String customerName) {
063: String selectHQL = "select contact.id from Contact contact";
064: selectHQL += " where contact.customer.name = :cName";
065:
066: List results = manager.createQuery(selectHQL).setFlushMode(
067: FlushModeType.AUTO).setParameter("cName", customerName)
068: .getResultList();
069:
070: return results;
071: }
072:
073: public List<Integer> getContactsByTLF(String tlf) {
074: String selectHQL = "select contact.id from Contact contact";
075: selectHQL += " where contact.tlf = :cTLF";
076:
077: List results = manager.createQuery(selectHQL).setFlushMode(
078: FlushModeType.AUTO).setParameter("cTLF", tlf)
079: .getResultList();
080:
081: return results;
082: }
083:
084: public int updateContacts(String name, String newTLF) {
085: String updateHQL = "update Contact set tlf = :cNewTLF where name = :cName";
086:
087: int rowsAffected = manager.createQuery(updateHQL).setFlushMode(
088: FlushModeType.AUTO).setParameter("cNewTLF", newTLF)
089: .setParameter("cName", name).executeUpdate();
090: return rowsAffected;
091: }
092:
093: public Contact getContact(Integer id) {
094: return manager.find(Contact.class, id);
095: }
096:
097: @Remove
098: public void remove() {
099:
100: }
101:
102: private Customer createCustomer(int id) {
103: System.out.println("CREATE CUSTOMER " + id);
104: try {
105: Customer customer = new Customer();
106: customer.setId(id);
107: customer.setName((id % 2 == 0) ? "JBoss" : "Red Hat");
108: Set<Contact> contacts = new HashSet<Contact>();
109:
110: Contact kabir = new Contact();
111: kabir.setId(1000 + id);
112: kabir.setCustomer(customer);
113: kabir.setName("Kabir");
114: kabir.setTlf("1111");
115: contacts.add(kabir);
116:
117: Contact bill = new Contact();
118: bill.setId(2000 + id);
119: bill.setCustomer(customer);
120: bill.setName("Bill");
121: bill.setTlf("2222");
122: contacts.add(bill);
123:
124: customer.setContacts(contacts);
125:
126: manager.persist(customer);
127: return customer;
128: } catch (RuntimeException e) {
129: throw e;
130: } catch (Exception e) {
131: throw new RuntimeException(e);
132: } finally {
133: System.out.println("CREATE CUSTOMER " + id + " - END");
134: }
135: }
136:
137: }
|