1 package org.hibernate.test.annotations; 3 4 import org.hibernate.Session; 5 import org.hibernate.Transaction; 6 import org.hibernate.test.annotations.inheritance.Carrot; 7 import org.hibernate.test.annotations.inheritance.Vegetable; 8 import org.hibernate.test.annotations.inheritance.VegetablePk; 9 import org.hibernate.test.annotations.inheritance.Tomato; 10 11 14 public class JoinedSubclassTest extends TestCase { 15 16 public JoinedSubclassTest(String x) { 17 super(x); 18 } 19 20 public void testDefaultValues() { 21 Session s; 22 Transaction tx; 23 s = openSession(); 24 tx = s.beginTransaction(); 25 Ferry f = new Ferry(); 26 f.setSize(2); 27 f.setSea("Channel"); 28 s.persist(f); 29 tx.commit(); 30 s.close(); 31 32 s = openSession(); 33 tx = s.beginTransaction(); 34 f = (Ferry) s.get(Ferry.class, f.getId() ); 35 assertNotNull(f); 36 assertEquals("Channel", f.getSea() ); 37 assertEquals(2, f.getSize() ); 38 tx.commit(); 39 s.close(); 40 } 41 42 public void testDeclaredValues() { 43 Session s; 44 Transaction tx; 45 s = openSession(); 46 tx = s.beginTransaction(); 47 Country c = new Country(); 48 c.setName("France"); 49 AmericaCupClass f = new AmericaCupClass(); 50 f.setSize(2); 51 f.country = c; 52 s.persist(c); 53 s.persist(f); 54 tx.commit(); 55 s.close(); 56 57 s = openSession(); 58 tx = s.beginTransaction(); 59 f = (AmericaCupClass) s.get(AmericaCupClass.class, f.getId() ); 60 assertNotNull(f); 61 assertEquals(c, f.country ); 62 assertEquals(2, f.getSize() ); 63 tx.commit(); 64 s.close(); 65 } 66 67 public void testCompositePk() throws Exception { 68 Session s; 69 Transaction tx; 70 s = openSession(); 71 tx = s.beginTransaction(); 72 Carrot c = new Carrot(); 73 VegetablePk pk = new VegetablePk(); 74 pk.setFarmer("Bill"); 75 pk.setHarvestDate("2004-08-15"); 76 c.setId(pk); 77 c.setLength(23); 78 s.persist(c); 79 tx.commit(); 80 s.close(); 81 82 s = openSession(); 83 tx = s.beginTransaction(); 84 Vegetable v = (Vegetable) s.createCriteria(Vegetable.class).uniqueResult(); 85 assertTrue(v instanceof Carrot); 86 Carrot result = (Carrot) v; 87 assertEquals( 23, result.getLength() ); 88 tx.commit(); 89 s.close(); 90 } 91 92 95 protected Class [] getMappings() { 96 return new Class [] { 97 Boat.class, 98 Ferry.class, 99 AmericaCupClass.class, 100 Country.class, 101 Vegetable.class, 102 Carrot.class, 103 Tomato.class 104 }; 105 } 106 107 } 108 | Popular Tags |