1 22 package org.jboss.ejb3.test.joininheritance; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 import javax.ejb.Remote ; 27 import javax.ejb.Stateless ; 28 import javax.persistence.EntityManager; 29 import javax.persistence.PersistenceContext; 30 import javax.persistence.PersistenceContext; 31 32 38 @Stateless 39 @Remote (EntityTest.class) 40 public class EntityTestBean implements EntityTest 41 { 42 private @PersistenceContext EntityManager manager; 43 44 private void assertTrue(boolean value) 45 { 46 if (!value) throw new RuntimeException ("assert failed"); 47 } 48 49 private void assertFalse(boolean value) 50 { 51 if (value) throw new RuntimeException ("assert failed"); 52 } 53 54 private void assertEquals(int x, int y) 55 { 56 if (x != y) throw new RuntimeException ("assert failed"); 57 } 58 59 private void assertEquals(String x, String y) 60 { 61 if (x == y) return; 62 if (x == null && y != null) throw new RuntimeException ("assert failed"); 63 if (x != null && y == null) throw new RuntimeException ("assert failed"); 64 if (!x.equals(y)) throw new RuntimeException ("assert failed"); 65 } 66 67 public long[] createBeans() throws Exception 68 { 69 Employee mark = new Employee(); 70 mark.setName("Mark"); 71 mark.setTitle("internal sales"); 72 mark.setSex('M'); 73 mark.setAddress("buckhead"); 74 mark.setZip("30305"); 75 mark.setCountry("USA"); 76 77 Customer joe = new Customer(); 78 joe.setName("Joe"); 79 joe.setAddress("San Francisco"); 80 joe.setZip("XXXXX"); 81 joe.setCountry("USA"); 82 joe.setComments("Very demanding"); 83 joe.setSalesperson(mark); 84 85 Person yomomma = new Person(); 86 yomomma.setName("mum"); 87 yomomma.setSex('F'); 88 89 manager.persist(mark); 90 manager.persist(joe); 91 manager.persist(yomomma); 92 long[] ids = {mark.getId(), joe.getId(), yomomma.getId()}; 93 return ids; 94 } 95 96 public void test1() throws Exception 97 { 98 assertEquals(manager.createQuery("from Person").getResultList().size(), 3); 99 assertEquals(manager.createQuery("from Person p where p.class = Customer").getResultList().size(), 1); 100 } 101 102 public void test2() throws Exception 103 { 104 List customers = manager.createQuery("from Customer c left join fetch c.salesperson").getResultList(); 105 for (Iterator iter = customers.iterator(); iter.hasNext();) 106 { 107 Customer c = (Customer) iter.next(); 108 assertEquals(c.getSalesperson().getName(), "Mark"); 109 } 110 assertEquals(customers.size(), 1); 111 } 112 113 public void test3() throws Exception 114 { 115 List customers = manager.createQuery("from Customer").getResultList(); 116 for (Iterator iter = customers.iterator(); iter.hasNext();) 117 { 118 Customer c = (Customer) iter.next(); 119 assertEquals(c.getSalesperson().getName(), "Mark"); 120 } 121 assertEquals(customers.size(), 1); 122 } 123 124 public void test4(long[] ids) throws Exception 125 { 126 Employee mark = manager.find(Employee.class, new Long (ids[0])); 127 Customer joe = (Customer) manager.find(Customer.class, new Long (ids[1])); 128 Person yomomma = manager.find(Person.class, new Long (ids[2])); 129 130 mark.setZip("30306"); 131 assertEquals(manager.createQuery("from Person p where p.zip = '30306'").getResultList().size(), 1); 132 manager.remove(mark); 133 manager.remove(joe); 134 manager.remove(yomomma); 135 assertTrue(manager.createQuery("from Person").getResultList().isEmpty()); 136 } 137 } 138 | Popular Tags |