Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. /*
  2.  * JBoss, Home of Professional Open Source.
  3.  * Copyright 2011, Red Hat, Inc., and individual contributors
  4.  * as indicated by the @author tags. See the copyright.txt file in the
  5.  * distribution for a 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.  
  23. package org.jboss.as.testsuite.integration.jpa.multiRow;
  24.  
  25. import javax.annotation.Resource;
  26. import javax.ejb.SessionContext;
  27. import javax.ejb.Stateful;
  28. import javax.ejb.TransactionManagement;
  29. import javax.ejb.TransactionManagementType;
  30. import javax.persistence.EntityManager;
  31. import javax.persistence.NoResultException;
  32. import javax.persistence.PersistenceContext;
  33. import javax.persistence.Query;
  34. import javax.transaction.UserTransaction;
  35.  
  36. /**
  37.  * stateful session bean
  38.  *
  39.  * @author Scott Marlow
  40.  */
  41. @Stateful
  42. @TransactionManagement(TransactionManagementType.BEAN)
  43. public class SFSB1 {
  44.     @PersistenceContext(unitName = "mypc")
  45.         EntityManager em;
  46.  
  47.     @Resource
  48.     SessionContext sessionContext;
  49.  
  50.     /**
  51.      * create the specified number of employees
  52.      * @param name
  53.      * @param address
  54.      * @param count
  55.      */
  56.     public void createEmployee(String name, String address, int startAt, int count) {
  57.  
  58.         UserTransaction tx1 = sessionContext.getUserTransaction();
  59.         try {
  60.             tx1.begin();
  61.             em.joinTransaction();
  62.             for (int id = startAt; id < count; id++) {
  63.                 Employee emp = new Employee();
  64.                 emp.setId(id);
  65.                 emp.setAddress(address + count);
  66.                 emp.setName(name + count);
  67.                 em.persist(emp);
  68.                 Employee2 emp2 = new Employee2();
  69.                 emp2.setId(id);
  70.                 emp2.setAddress(address + count);
  71.                 emp2.setName(name + count);
  72.                 em.persist(emp2);
  73.                 Employee3 emp3 = new Employee3();
  74.                 emp3.setId(id);
  75.                 emp3.setAddress(address + count);
  76.                 emp3.setName(name + count);
  77.                 em.persist(emp3);
  78.                 Employee4 emp4 = new Employee4();
  79.                 emp4.setId(id);
  80.                 emp4.setAddress(address + count);
  81.                 emp4.setName(name + count);
  82.                 em.persist(emp4);
  83.                 Employee5 emp5 = new Employee5();
  84.                 emp5.setId(id);
  85.                 emp5.setAddress(address + count);
  86.                 emp5.setName(name + count);
  87.                 em.persist(emp5);
  88.  
  89.  
  90.             }
  91.             tx1.commit();
  92.  
  93.         } catch (Exception e) {
  94.             throw new RuntimeException("couldn't start tx" , e);
  95.         }
  96.  
  97.     }
  98.  
  99.  
  100.     public Employee getEmployeeNoTX(int id) {
  101.  
  102.         return em.find(Employee.class, id);
  103.     }
  104.  
  105.     public String queryEmployeeNameNoTX(int id) {
  106.         Query q = em.createQuery("SELECT e.name FROM Employee e");
  107.         try {
  108.             String name = (String)q.getSingleResult();
  109.             return name;
  110.         }
  111.         catch (NoResultException expected) {
  112.             return "success";
  113.         }
  114.         catch (Exception unexpected) {
  115.             return unexpected.getMessage();
  116.         }
  117.  
  118.     }
  119.  
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement