1 package org.hibernate.test.annotations.inheritance; 3 4 import java.util.List ; 5 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 import org.hibernate.Transaction; 9 import org.hibernate.test.annotations.A320; 10 import org.hibernate.test.annotations.A320b; 11 import org.hibernate.test.annotations.Plane; 12 import org.hibernate.test.annotations.TestCase; 13 import org.hibernate.test.annotations.inheritance.singletable.Funk; 14 import org.hibernate.test.annotations.inheritance.singletable.Music; 15 import org.hibernate.test.annotations.inheritance.singletable.Noise; 16 import org.hibernate.test.annotations.inheritance.singletable.Rock; 17 18 21 public class SubclassTest extends TestCase { 22 23 public SubclassTest(String x) { 24 super(x); 25 } 26 27 public void testPolymorphism() throws Exception { 28 Session s = openSession(); 29 Transaction tx = s.beginTransaction(); 30 Plane p = new Plane(); 31 p.setNbrOfSeats(10); 32 A320 a = new A320(); 33 a.setJavaEmbeddedVersion("5.0"); 34 a.setNbrOfSeats(300); 35 s.persist(a); 36 s.persist(p); 37 tx.commit(); 38 s.close(); 39 40 s = openSession(); 41 tx = s.beginTransaction(); 42 Query q = s.createQuery("from " + A320.class.getName() ); 43 List a320s = q.list(); 44 assertNotNull(a320s); 45 assertEquals( 1, a320s.size() ); 46 assertTrue(a320s.get(0) instanceof A320); 47 assertEquals( "5.0", ( (A320) a320s.get(0) ).getJavaEmbeddedVersion()); 48 q = s.createQuery("from " + Plane.class.getName() ); 49 List planes = q.list(); 50 assertNotNull(planes); 51 assertEquals( 2, planes.size() ); 52 tx.commit(); 53 s.close(); 54 } 55 public void test2ndLevelSubClass() throws Exception { 56 Session s = openSession(); 57 Transaction tx = s.beginTransaction(); 58 A320b a = new A320b(); 59 a.setJavaEmbeddedVersion("Elephant"); 60 a.setNbrOfSeats(300); 61 s.persist(a); 62 tx.commit(); 63 s.close(); 64 65 s = openSession(); 66 tx = s.beginTransaction(); 67 Query q = s.createQuery("from " + A320.class.getName() + " as a where a.javaEmbeddedVersion = :version"); 68 q.setString("version", "Elephant"); 69 List a320s = q.list(); 70 assertNotNull(a320s); 71 assertEquals( 1, a320s.size() ); 72 tx.commit(); 73 s.close(); 74 } 75 76 public void testEmbeddedSuperclass() throws Exception { 77 Session s = openSession(); 78 Transaction tx = s.beginTransaction(); 79 Plane p = new Plane(); 80 p.setAltitude(10000); 81 p.setMetricAltitude(3000); 82 p.setNbrOfSeats(150); 83 s.persist(p); 84 tx.commit(); 85 s.close(); 86 87 s = openSession(); 88 tx = s.beginTransaction(); 89 p = (Plane) s.get( Plane.class, p.getId() ); 90 assertNotNull(p); 91 assertEquals( 150, p.getNbrOfSeats() ); 92 assertEquals( 10000, p.getAltitude() ); 93 assertFalse( 3000 == p.getMetricAltitude() ); 94 tx.commit(); 95 s.close(); 96 } 97 98 public void testDefault() throws Exception { 99 Session s; 100 Transaction tx; 101 s = openSession(); 102 tx = s.beginTransaction(); 103 Fruit f = new Fruit(); 104 Apple a = new Apple(); 105 s.persist(f); 106 s.persist(a); 107 tx.commit(); 108 s.close(); 109 110 s = openSession(); 111 tx = s.beginTransaction(); 112 List result = s.createCriteria(Fruit.class).list(); 113 assertNotNull(result); 114 assertEquals( 2, result.size() ); 115 Fruit f2 = (Fruit) result.get(0); 116 checkClassType(f2, f, a); 117 f2 = (Fruit) result.get(1); 118 checkClassType(f2, f, a); 119 tx.commit(); 120 s.close(); 121 } 122 123 public void testFormula() throws Exception { 124 Session s; 125 Transaction tx; 126 s = openSession(); 127 tx = s.beginTransaction(); 128 Rock guns = new Rock(); 129 guns.setAvgBeat(90); 130 guns.setType(2); 131 Noise white = new Noise(); 132 white.setAvgBeat(0); 133 white.setType(null); 134 135 s.persist(guns); 136 s.persist(white); 137 tx.commit(); 138 s.close(); 139 140 s = openSession(); 141 tx = s.beginTransaction(); 142 List result = s.createCriteria(Noise.class).list(); 143 assertNotNull(result); 144 assertEquals( 1, result.size() ); 145 white = (Noise) result.get(0); 146 assertNull( white.getType() ); 147 s.delete( white ); 148 result = s.createCriteria(Rock.class).list(); 149 assertEquals( 1, result.size() ); 150 s.delete( result.get(0) ); 151 result = s.createCriteria(Funk.class).list(); 152 assertEquals( 0, result.size() ); 153 154 tx.commit(); 155 s.close(); 156 } 157 158 private void checkClassType(Fruit fruitToTest, Fruit f, Apple a) { 159 if ( fruitToTest.getId().equals( f.getId() ) ) { 160 assertFalse(fruitToTest instanceof Apple); 161 } else if ( fruitToTest.getId().equals( a.getId() ) ) { 162 assertTrue(fruitToTest instanceof Apple); 163 } else { 164 fail("Result does not contains the previously inserted elements"); 165 } 166 } 167 168 171 protected Class [] getMappings() { 172 return new Class [] { 173 A320b.class, Plane.class, 175 A320.class, 176 Fruit.class, 177 Apple.class, 179 Music.class, 180 Rock.class, 181 Funk.class, 182 Noise.class 183 }; 184 } 185 186 } 187 | Popular Tags |