1 package org.hibernate.test.subselect; 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.Session; 11 import org.hibernate.Transaction; 12 import org.hibernate.test.TestCase; 13 14 17 public class SubselectTest extends TestCase { 18 19 public SubselectTest(String str) { 20 super(str); 21 } 22 23 public void testEntitySubselect() { 24 Session s = openSession(); 25 Transaction t = s.beginTransaction(); 26 Human gavin = new Human(); 27 gavin.setName( "gavin" ); 28 gavin.setSex( 'M' ); 29 gavin.setAddress( "Melbourne, Australia" ); 30 Alien x23y4 = new Alien(); 31 x23y4.setIdentity( "x23y4$$hu%3" ); 32 x23y4.setPlanet( "Mars" ); 33 x23y4.setSpecies( "martian" ); 34 s.save(gavin); 35 s.save(x23y4); 36 s.flush(); 37 List beings = s.createQuery("from Being").list(); 38 for ( Iterator iter = beings.iterator(); iter.hasNext(); ) { 39 Being b = (Being) iter.next(); 40 assertNotNull( b.getLocation() ); 41 assertNotNull( b.getIdentity() ); 42 assertNotNull( b.getSpecies() ); 43 } 44 s.clear(); 45 getSessions().evict(Being.class); 46 Being gav = (Being) s.get(Being.class, gavin.getId()); 47 assertEquals( gav.getLocation(), gavin.getAddress() ); 48 assertEquals( gav.getSpecies(), "human" ); 49 assertEquals( gav.getIdentity(), gavin.getName() ); 50 s.clear(); 51 gavin = (Human) s.get(Human.class, gavin.getId()); 53 gavin.setAddress( "Atlanta, GA" ); 54 gav = (Being) s.createQuery("from Being b where b.location like '%GA%'").uniqueResult(); 55 assertEquals( gav.getLocation(), gavin.getAddress() ); 56 s.delete(gavin); 57 s.delete(x23y4); 58 assertTrue( s.createQuery("from Being").list().isEmpty() ); 59 t.commit(); 60 s.close(); 61 } 62 63 64 protected String [] getMappings() { 65 return new String [] { "subselect/Beings.hbm.xml" }; 66 } 67 68 public static Test suite() { 69 return new TestSuite(SubselectTest.class); 70 } 71 72 } 73 74 | Popular Tags |