1 package org.hibernate.test.subclassfilter; 3 4 import org.hibernate.test.TestCase; 5 import org.hibernate.classic.Session; 6 import org.hibernate.Transaction; 7 8 import java.util.List ; 9 import java.util.Iterator ; 10 import java.util.ArrayList ; 11 import java.util.HashSet ; 12 13 import junit.framework.Test; 14 import junit.framework.TestSuite; 15 16 21 public class UnionSubclassFilterTest extends TestCase { 22 23 public UnionSubclassFilterTest(String name) { 24 super( name ); 25 } 26 27 protected final String [] getMappings() { 28 return new String [] { "subclassfilter/union-subclass.hbm.xml" }; 29 } 30 31 public void testFiltersWithUnionSubclass() { 32 Session s = openSession(); 33 s.enableFilter( "region" ).setParameter( "userRegion", "US" ); 34 Transaction t = s.beginTransaction(); 35 36 prepareTestData( s ); 37 s.clear(); 38 39 List results; 40 Iterator itr; 41 42 results = s.createQuery( "from Person" ).list(); 43 assertEquals( "Incorrect qry result count", 4, results.size() ); 44 s.clear(); 45 46 results = s.createQuery( "from Employee" ).list(); 47 assertEquals( "Incorrect qry result count", 2, results.size() ); 48 s.clear(); 49 50 results = new ArrayList ( new HashSet ( s.createQuery( "from Person as p left join fetch p.minions" ).list() ) ); 51 assertEquals( "Incorrect qry result count", 4, results.size() ); 52 itr = results.iterator(); 53 while ( itr.hasNext() ) { 54 final Person p = ( Person ) itr.next(); 56 if ( p.getName().equals( "John Doe" ) ) { 57 Employee john = ( Employee ) p; 58 assertEquals( "Incorrect fecthed minions count", 1, john.getMinions().size() ); 59 break; 60 } 61 } 62 s.clear(); 63 64 results = new ArrayList ( new HashSet ( s.createQuery( "from Employee as p left join fetch p.minions" ).list() ) ); 65 assertEquals( "Incorrect qry result count", 2, results.size() ); 66 itr = results.iterator(); 67 while ( itr.hasNext() ) { 68 final Person p = ( Person ) itr.next(); 70 if ( p.getName().equals( "John Doe" ) ) { 71 Employee john = ( Employee ) p; 72 assertEquals( "Incorrect fecthed minions count", 1, john.getMinions().size() ); 73 break; 74 } 75 } 76 77 t.commit(); 78 s.close(); 79 } 80 81 private void prepareTestData(Session s) { 82 Employee john = new Employee("John Doe"); 83 john.setCompany( "JBoss" ); 84 john.setDepartment( "hr" ); 85 john.setTitle( "hr guru" ); 86 john.setRegion( "US" ); 87 88 Employee polli = new Employee("Polli Wog"); 89 polli.setCompany( "JBoss" ); 90 polli.setDepartment( "hr" ); 91 polli.setTitle( "hr novice" ); 92 polli.setRegion( "US" ); 93 polli.setManager( john ); 94 john.getMinions().add( polli ); 95 96 Employee suzie = new Employee( "Suzie Q" ); 97 suzie.setCompany( "JBoss" ); 98 suzie.setDepartment( "hr" ); 99 suzie.setTitle( "hr novice" ); 100 suzie.setRegion( "EMEA" ); 101 suzie.setManager( john ); 102 john.getMinions().add( suzie ); 103 104 Customer cust = new Customer( "John Q Public" ); 105 cust.setCompany( "Acme" ); 106 cust.setRegion( "US" ); 107 cust.setContactOwner( john ); 108 109 Person ups = new Person( "UPS guy" ); 110 ups.setCompany( "UPS" ); 111 ups.setRegion( "US" ); 112 113 s.save( john ); 114 s.save( cust ); 115 s.save( ups ); 116 117 s.flush(); 118 } 119 120 public static Test suite() { 121 return new TestSuite(UnionSubclassFilterTest.class); 122 } 123 } 124 | Popular Tags |