1 package org.hibernate.test.propertyref; 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.FetchMode; 11 import org.hibernate.Hibernate; 12 import org.hibernate.Session; 13 import org.hibernate.Transaction; 14 import org.hibernate.cfg.Configuration; 15 import org.hibernate.cfg.Environment; 16 import org.hibernate.mapping.Column; 17 import org.hibernate.mapping.ForeignKey; 18 import org.hibernate.mapping.PersistentClass; 19 import org.hibernate.test.TestCase; 20 21 24 public class PropertyRefTest extends TestCase { 25 26 public PropertyRefTest(String str) { 27 super(str); 28 } 29 30 public void testManyToManyPropertyRef() { 31 Session s = openSession(); 32 Transaction t = s.beginTransaction(); 33 Person p = new Person(); 34 p.setName("Steve"); 35 p.setUserId("steve"); 36 s.persist(p); 37 Person p2 = new Person(); 38 p2.setName("Max"); 39 p2.setUserId("max"); 40 s.persist(p2); 41 Group g = new Group(); 42 g.setName("Admins"); 43 g.getUsers().add(p); 44 g.getUsers().add(p2); 45 s.persist(g); 46 s.flush(); 47 s.clear(); 48 g = (Group) s.createQuery("from Group g left join fetch g.users").uniqueResult(); 49 assertTrue( Hibernate.isInitialized( g.getUsers() ) ); 50 assertEquals( 2, g.getUsers().size() ); 51 s.delete(g); 52 s.createQuery("delete Person").executeUpdate(); 53 t.commit(); 54 s.close(); 55 } 56 57 public void testOneToOnePropertyRef() { 58 Session s = openSession(); 59 Transaction t = s.beginTransaction(); 60 Person p = new Person(); 61 p.setName("Steve"); 62 p.setUserId("steve"); 63 Address a = new Address(); 64 a.setAddress("Texas"); 65 a.setCountry("USA"); 66 p.setAddress(a); 67 a.setPerson(p); 68 s.save(p); 69 Person p2 = new Person(); 70 p2.setName("Max"); 71 p2.setUserId("max"); 72 s.save(p2); 73 Account act = new Account(); 74 act.setType('c'); 75 act.setUser(p2); 76 p2.getAccounts().add(act); 77 s.save(act); 78 s.flush(); 79 s.clear(); 80 81 p = (Person) s.get( Person.class, p.getId() ); p2 = (Person) s.get( Person.class, p2.getId() ); assertNull( p2.getAddress() ); 84 assertNotNull( p.getAddress() ); 85 List l = s.createQuery("from Person").list(); assertEquals( l.size(), 2 ); 87 assertTrue( l.contains(p) && l.contains(p2) ); 88 s.clear(); 89 90 l = s.createQuery("from Person p order by p.name").list(); assertEquals( l.size(), 2 ); 92 assertNull( ( (Person) l.get(0) ).getAddress() ); 93 assertNotNull( ( (Person) l.get(1) ).getAddress() ); 94 s.clear(); 95 96 l = s.createQuery("from Person p left join fetch p.address a order by a.country").list(); assertEquals( l.size(), 2 ); 98 if ( ( (Person) l.get(0) ).getName().equals("Max") ) { 99 assertNull( ( (Person) l.get(0) ).getAddress() ); 100 assertNotNull( ( (Person) l.get(1) ).getAddress() ); 101 } 102 else { 103 assertNull( ( (Person) l.get(1) ).getAddress() ); 104 assertNotNull( ( (Person) l.get(0) ).getAddress() ); 105 } 106 s.clear(); 107 108 l = s.createQuery("from Person p left join p.accounts a").list(); 109 for ( int i=0; i<2; i++ ) { 110 Object [] row = (Object []) l.get(i); 111 Person px = (Person) row[0]; 112 assertFalse( Hibernate.isInitialized( px.getAccounts() ) ); 113 assertTrue( px.getAccounts().size()>0 || row[1]==null ); 114 } 115 s.clear(); 116 117 l = s.createQuery("from Person p left join fetch p.accounts a order by p.name").list(); 118 Person p0 = (Person) l.get(0); 119 assertTrue( Hibernate.isInitialized( p0.getAccounts() ) ); 120 assertEquals( p0.getAccounts().size(), 1 ); 121 assertSame( ( (Account) p0.getAccounts().iterator().next() ).getUser(), p0 ); 122 Person p1 = (Person) l.get(1); 123 assertTrue( Hibernate.isInitialized( p1.getAccounts() ) ); 124 assertEquals( p1.getAccounts().size(), 0 ); 125 126 s.createQuery("delete from Address").executeUpdate(); 127 s.createQuery("delete from Account").executeUpdate(); s.createQuery("delete from Person").executeUpdate(); 129 130 t.commit(); 131 s.close(); 132 } 133 134 135 public void testJoinFetchPropertyRef() { 136 Session s = openSession(); 137 Transaction t = s.beginTransaction(); 138 Person p = new Person(); 139 p.setName("Steve"); 140 p.setUserId("steve"); 141 Address a = new Address(); 142 a.setAddress("Texas"); 143 a.setCountry("USA"); 144 p.setAddress(a); 145 a.setPerson(p); 146 s.save(p); 147 148 s.flush(); 149 s.clear(); 150 151 getSessions().getStatistics().clear(); 152 153 p = (Person) s.get( Person.class, p.getId() ); 155 assertTrue( Hibernate.isInitialized( p.getAddress() ) ); 156 assertNotNull( p.getAddress() ); 157 assertEquals( getSessions().getStatistics().getPrepareStatementCount(), 1 ); 158 assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 ); 159 160 s.clear(); 161 162 getSessions().getStatistics().clear(); 163 164 p = (Person) s.createCriteria(Person.class) 165 .setFetchMode("address", FetchMode.SELECT) 166 .uniqueResult(); 168 assertTrue( Hibernate.isInitialized( p.getAddress() ) ); 169 assertNotNull( p.getAddress() ); 170 assertEquals( getSessions().getStatistics().getPrepareStatementCount(), 2 ); 171 assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 ); 172 173 s.createQuery("delete from Address").executeUpdate(); 174 s.createQuery("delete from Person").executeUpdate(); 175 176 t.commit(); 177 s.close(); 178 } 179 180 public void testForeignKeyCreation() { 181 PersistentClass classMapping = getCfg().getClassMapping("org.hibernate.test.propertyref.Account"); 182 183 Iterator foreignKeyIterator = classMapping.getTable().getForeignKeyIterator(); 184 boolean found = false; 185 while ( foreignKeyIterator.hasNext() ) { 186 ForeignKey element = (ForeignKey) foreignKeyIterator.next(); 187 if(element.getReferencedEntityName().equals(Person.class.getName() ) ) { 188 189 if(!element.isReferenceToPrimaryKey() ) { 190 List referencedColumns = element.getReferencedColumns(); 191 Column column = (Column) referencedColumns.get(0); 192 if(column.getName().equals("person_userid") ) { 193 found = true; } 195 } 196 } 197 } 198 199 assertTrue("Property ref foreign key not found",found); 200 } 201 202 protected String [] getMappings() { 203 return new String [] { "propertyref/Person.hbm.xml" }; 204 } 205 206 public static Test suite() { 207 return new TestSuite(PropertyRefTest.class); 208 } 209 210 protected void configure(Configuration cfg) { 211 cfg.setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE, "1"); 212 cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); 213 } 214 } 215 216 | Popular Tags |