1 package org.hibernate.test.discriminator; 3 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import junit.framework.Test; 8 import junit.framework.TestSuite; 9 10 import org.hibernate.Hibernate; 11 import org.hibernate.Session; 12 import org.hibernate.Transaction; 13 import org.hibernate.test.TestCase; 14 15 18 public class DiscriminatorTest extends TestCase { 19 20 public DiscriminatorTest(String str) { 21 super(str); 22 } 23 24 public void testJoinedSubclass() { 25 Session s = openSession(); 26 Transaction t = s.beginTransaction(); 27 28 Employee mark = new Employee(); 29 mark.setName("Mark"); 30 mark.setTitle("internal sales"); 31 mark.setSex('M'); 32 mark.setAddress("buckhead"); 33 mark.setZip("30305"); 34 mark.setCountry("USA"); 35 36 Customer joe = new Customer(); 37 joe.setName("Joe"); 38 joe.setAddress("San Francisco"); 39 joe.setZip("XXXXX"); 40 joe.setCountry("USA"); 41 joe.setComments("Very demanding"); 42 joe.setSex('M'); 43 joe.setSalesperson(mark); 44 45 Person yomomma = new Person(); 46 yomomma.setName("mum"); 47 yomomma.setSex('F'); 48 49 s.save(yomomma); 50 s.save(mark); 51 s.save(joe); 52 53 assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 ); 54 55 assertEquals( s.createQuery("from Person").list().size(), 3 ); 56 assertEquals( s.createQuery("from Person p where p.class = Person").list().size(), 1 ); 57 assertEquals( s.createQuery("from Person p where p.class = Customer").list().size(), 1 ); 58 s.clear(); 59 60 List customers = s.createQuery("from Customer c left join fetch c.salesperson").list(); 61 for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { 62 Customer c = (Customer) iter.next(); 63 assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); 64 assertEquals( c.getSalesperson().getName(), "Mark" ); 65 } 66 assertEquals( customers.size(), 1 ); 67 s.clear(); 68 69 customers = s.createQuery("from Customer").list(); 70 for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { 71 Customer c = (Customer) iter.next(); 72 assertFalse( Hibernate.isInitialized( c.getSalesperson() ) ); 73 assertEquals( c.getSalesperson().getName(), "Mark" ); 74 } 75 assertEquals( customers.size(), 1 ); 76 s.clear(); 77 78 79 mark = (Employee) s.get( Employee.class, new Long ( mark.getId() ) ); 80 joe = (Customer) s.get( Customer.class, new Long ( joe.getId() ) ); 81 82 mark.setZip("30306"); 83 assertEquals( s.createQuery("from Person p where p.address.zip = '30306'").list().size(), 1 ); 84 s.delete(mark); 85 s.delete(joe); 86 s.delete(yomomma); 87 assertTrue( s.createQuery("from Person").list().isEmpty() ); 88 t.commit(); 89 s.close(); 90 } 91 92 93 protected String [] getMappings() { 94 return new String [] { "discriminator/Person.hbm.xml" }; 95 } 96 97 public static Test suite() { 98 return new TestSuite(DiscriminatorTest.class); 99 } 100 101 } 102 103 | Popular Tags |