1 package org.hibernate.test.unionsubclass2; 3 4 import java.math.BigDecimal ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import junit.framework.Test; 9 import junit.framework.TestSuite; 10 11 import org.hibernate.Hibernate; 12 import org.hibernate.Session; 13 import org.hibernate.Transaction; 14 import org.hibernate.criterion.Expression; 15 import org.hibernate.criterion.Property; 16 import org.hibernate.dialect.HSQLDialect; 17 import org.hibernate.test.TestCase; 18 19 22 public class UnionSubclassTest extends TestCase { 23 24 public UnionSubclassTest(String str) { 25 super(str); 26 } 27 28 public void testJoinedSubclass() { 29 Session s = openSession(); 30 Transaction t = s.beginTransaction(); 31 32 Employee mark = new Employee(); 33 mark.setName("Mark"); 34 mark.setTitle("internal sales"); 35 mark.setSex('M'); 36 mark.setAddress("buckhead"); 37 mark.setZip("30305"); 38 mark.setCountry("USA"); 39 40 Customer joe = new Customer(); 41 joe.setName("Joe"); 42 joe.setAddress("San Francisco"); 43 joe.setZip("XXXXX"); 44 joe.setCountry("USA"); 45 joe.setComments("Very demanding"); 46 joe.setSex('M'); 47 joe.setSalesperson(mark); 48 49 Person yomomma = new Person(); 50 yomomma.setName("mum"); 51 yomomma.setSex('F'); 52 53 s.save(yomomma); 54 s.save(mark); 55 s.save(joe); 56 57 assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 ); 58 59 assertEquals( s.createQuery("from Person").list().size(), 3 ); 60 assertEquals( s.createQuery("from Person p where p.class = Customer").list().size(), 1 ); 61 assertEquals( s.createQuery("from Person p where p.class = Person").list().size(), 1 ); 62 s.clear(); 63 64 List customers = s.createQuery("from Customer c left join fetch c.salesperson").list(); 65 for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { 66 Customer c = (Customer) iter.next(); 67 assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); 68 assertEquals( c.getSalesperson().getName(), "Mark" ); 69 } 70 assertEquals( customers.size(), 1 ); 71 s.clear(); 72 73 customers = s.createQuery("from Customer").list(); 74 for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { 75 Customer c = (Customer) iter.next(); 76 assertFalse( Hibernate.isInitialized( c.getSalesperson() ) ); 77 assertEquals( c.getSalesperson().getName(), "Mark" ); 78 } 79 assertEquals( customers.size(), 1 ); 80 s.clear(); 81 82 83 mark = (Employee) s.get( Employee.class, new Long ( mark.getId() ) ); 84 joe = (Customer) s.get( Customer.class, new Long ( joe.getId() ) ); 85 86 mark.setZip("30306"); 87 assertEquals( s.createQuery("from Person p where p.address.zip = '30306'").list().size(), 1 ); 88 89 if ( !(getDialect() instanceof HSQLDialect) ) { 90 s.createCriteria(Person.class).add( 91 Expression.in("address", new Address[] { mark.getAddress(), joe.getAddress() } ) 92 ).list(); 93 } 94 95 s.delete(mark); 96 s.delete(joe); 97 s.delete(yomomma); 98 assertTrue( s.createQuery("from Person").list().isEmpty() ); 99 t.commit(); 100 s.close(); 101 } 102 103 public void testQuerySubclassAttribute() { 104 if ( getDialect() instanceof HSQLDialect ) return; 106 Session s = openSession(); 107 Transaction t = s.beginTransaction(); 108 Person p = new Person(); 109 p.setName("Emmanuel"); 110 p.setSex('M'); 111 s.persist(p); 112 Employee q = new Employee(); 113 q.setName("Steve"); 114 q.setSex('M'); 115 q.setTitle("Mr"); 116 q.setSalary( new BigDecimal (1000) ); 117 s.persist(q); 118 119 List result = s.createQuery("from Person where salary > 100").list(); 120 assertEquals( result.size(), 1 ); 121 assertSame( result.get(0), q ); 122 123 result = s.createQuery("from Person where salary > 100 or name like 'E%'").list(); 124 assertEquals( result.size(), 2 ); 125 126 result = s.createCriteria(Person.class) 127 .add( Property.forName("salary").gt( new BigDecimal (100) ) ) 128 .list(); 129 assertEquals( result.size(), 1 ); 130 assertSame( result.get(0), q ); 131 132 result = s.createQuery("select salary from Person where salary > 100").list(); 133 assertEquals( result.size(), 1 ); 134 assertEquals( ( (BigDecimal ) result.get(0) ).intValue(), 1000 ); 135 136 s.delete(p); 137 s.delete(q); 138 t.commit(); 139 s.close(); 140 } 141 142 143 protected String [] getMappings() { 144 return new String [] { "unionsubclass2/Person.hbm.xml" }; 145 } 146 147 public static Test suite() { 148 return new TestSuite(UnionSubclassTest.class); 149 } 150 151 } 152 153 | Popular Tags |