1 package org.hibernate.test.annotations.entity; 3 4 import java.util.Date ; 5 import java.util.Currency ; 6 import java.math.BigDecimal ; 7 8 import org.hibernate.Hibernate; 9 import org.hibernate.HibernateException; 10 import org.hibernate.Query; 11 import org.hibernate.Session; 12 import org.hibernate.Transaction; 13 import org.hibernate.test.annotations.TestCase; 14 15 18 public class BasicHibernateAnnotationsTest extends TestCase { 19 20 public void testEntity() throws Exception { 21 Forest forest = new Forest(); 22 forest.setName("Fontainebleau"); 23 Session s; 24 Transaction tx; 25 s = openSession(); 26 tx = s.beginTransaction(); 27 s.persist(forest); 28 tx.commit(); 29 s.close(); 30 31 s = openSession(); 32 tx = s.beginTransaction(); 33 forest = (Forest) s.get( Forest.class, forest.getId() ); 34 assertNotNull(forest); 35 forest.setName("Fontainebleau"); 36 tx.commit(); 38 s.close(); 39 40 s = openSession(); 41 tx = s.beginTransaction(); 42 forest = (Forest) s.get( Forest.class, forest.getId() ); 43 assertNotNull(forest); 44 forest.setLength(23); 45 tx.commit(); 47 s.close(); 48 } 49 50 public void testVersioning() throws Exception { 51 Forest forest = new Forest(); 52 forest.setName("Fontainebleau"); 53 forest.setLength(33); 54 Session s; 55 Transaction tx; 56 s = openSession(); 57 tx = s.beginTransaction(); 58 s.persist(forest); 59 tx.commit(); 60 s.close(); 61 62 Session parallelSession = openSession(); 63 Transaction parallelTx = parallelSession.beginTransaction(); 64 s = openSession(); 65 tx = s.beginTransaction(); 66 67 forest = (Forest) parallelSession.get(Forest.class, forest.getId() ); 68 Forest reloadedForest = (Forest) s.get(Forest.class, forest.getId() ); 69 reloadedForest.setLength(11); 70 assertNotSame(forest, reloadedForest); 71 tx.commit(); 72 s.close(); 73 74 forest.setLength(22); 75 try { 76 parallelTx.commit(); 77 fail("All optimistic locking should have make it fail"); 78 } 79 catch (HibernateException e) { 80 81 } 82 parallelSession.close(); 83 } 84 85 public void testPolymorphism() throws Exception { 86 Forest forest = new Forest(); 87 forest.setName("Fontainebleau"); 88 forest.setLength(33); 89 Session s; 90 Transaction tx; 91 s = openSession(); 92 tx = s.beginTransaction(); 93 s.persist(forest); 94 tx.commit(); 95 s.close(); 96 97 s = openSession(); 98 tx = s.beginTransaction(); 99 Query query = s.createQuery("from java.lang.Object"); 100 assertEquals( 0, query.list().size() ); 101 query = s.createQuery("from Forest"); 102 assertTrue( 0 < query.list().size() ); 103 tx.commit(); 104 s.close(); 105 } 106 107 public void testType() throws Exception { 108 Forest f = new Forest(); 109 f.setName("Broceliande"); 110 String description = "C'est une enorme foret enchantée où vivais Merlin et toute la clique"; 111 f.setLongDescription(description); 112 Session s; 113 Transaction tx; 114 s = openSession(); 115 tx = s.beginTransaction(); 116 s.persist(f); 117 tx.commit(); 118 s.close(); 119 120 s = openSession(); 121 tx = s.beginTransaction(); 122 f = (Forest) s.get( Forest.class, f.getId() ); 123 assertNotNull(f); 124 assertEquals( description, f.getLongDescription() ); 125 tx.commit(); 126 s.close(); 127 128 } 129 130 public void testNonLazy() throws Exception { 131 Session s; 132 Transaction tx; 133 s = openSession(); 134 tx = s.beginTransaction(); 135 Forest f = new Forest(); 136 Tree t = new Tree(); 137 t.setName("Basic one"); 138 s.persist(f); 139 s.persist(t); 140 tx.commit(); 141 s.close(); 142 143 s = openSession(); 144 tx = s.beginTransaction(); 145 f = (Forest) s.load( Forest.class, f.getId() ); 146 t = (Tree) s.load( Tree.class, t.getId() ); 147 assertFalse( "Default should be lazy", Hibernate.isInitialized(f) ); 148 assertTrue( "Tree is not lazy", Hibernate.isInitialized(t) ); 149 tx.commit(); 150 s.close(); 151 } 152 153 public void testCache() throws Exception { 154 Session s; 155 Transaction tx; 156 s = openSession(); 157 tx = s.beginTransaction(); 158 ZipCode zc = new ZipCode(); 159 zc.code = "92400"; 160 s.persist(zc); 161 tx.commit(); 162 s.close(); 163 getSessions().getStatistics().clear(); 164 getSessions().getStatistics().setStatisticsEnabled(true); 165 getSessions().evict(ZipCode.class); 166 s = openSession(); 167 tx = s.beginTransaction(); 168 s.get( ZipCode.class, zc.code ); 169 assertEquals( 1, getSessions().getStatistics().getSecondLevelCachePutCount() ); 170 tx.commit(); 171 s.close(); 172 173 s = openSession(); 174 tx = s.beginTransaction(); 175 s.get( ZipCode.class, zc.code ); 176 assertEquals( 1, getSessions().getStatistics().getSecondLevelCacheHitCount() ); 177 tx.commit(); 178 s.close(); 179 } 180 181 public void testFilter() throws Exception { 182 Session s; 183 Transaction tx; 184 s = openSession(); 185 tx = s.beginTransaction(); 186 s.createQuery("delete Forest").executeUpdate(); 187 Forest f1 = new Forest(); 188 f1.setLength(2); 189 s.persist(f1); 190 Forest f2 = new Forest(); 191 f2.setLength(20); 192 s.persist(f2); 193 Forest f3 = new Forest(); 194 f3.setLength(200); 195 s.persist(f3); 196 tx.commit(); 197 s.close(); 198 s = openSession(); 199 tx = s.beginTransaction(); 200 s.enableFilter("betweenLength").setParameter("minLength", 5).setParameter("maxLength", 50); 201 int count = ( (Integer ) s.createQuery("select count(*) from Forest").iterate().next() ).intValue(); 202 assertEquals(1, count); 203 s.disableFilter("betweenLength"); 204 s.enableFilter("minLength").setParameter("minLength", 5); 205 count = ( (Integer ) s.createQuery("select count(*) from Forest").iterate().next() ).intValue(); 206 assertEquals(2, count); 207 s.disableFilter("minLength"); 208 tx.rollback(); 209 s.close(); 210 } 211 212 public void testParameterizedType() throws Exception { 213 Session s; 214 Transaction tx; 215 s = openSession(); 216 tx = s.beginTransaction(); 217 Forest f = new Forest(); 218 f.setSmallText("ThisIsASmallText"); 219 f.setBigText("ThisIsABigText"); 220 s.persist(f); 221 tx.commit(); 222 s.close(); 223 s = openSession(); 224 tx = s.beginTransaction(); 225 Forest f2 = (Forest) s.get( Forest.class, f.getId() ); 226 assertEquals( f.getSmallText().toLowerCase(), f2.getSmallText() ); 227 assertEquals( f.getBigText().toUpperCase(), f2.getBigText() ); 228 tx.commit(); 229 s.close(); 230 } 231 232 public void testSerialized() throws Exception { 233 Forest forest = new Forest(); 234 forest.setName("Shire"); 235 Country country = new Country(); 236 country.setName("Middle Earth"); 237 forest.setCountry(country); 238 Session s; 239 Transaction tx; 240 s = openSession(); 241 tx = s.beginTransaction(); 242 s.persist(forest); 243 tx.commit(); 244 s.close(); 245 246 s = openSession(); 247 tx = s.beginTransaction(); 248 forest = (Forest) s.get( Forest.class, forest.getId() ); 249 assertNotNull(forest); 250 assertNotNull( forest.getCountry() ); 251 assertEquals( country.getName(), forest.getCountry().getName() ); 252 tx.commit(); 253 s.close(); 254 } 255 256 public void testCompositeType() throws Exception { 257 Session s; 258 Transaction tx; 259 s = openSession(); 260 tx = s.beginTransaction(); 261 Ransom r = new Ransom(); 262 r.setKidnapperName("Se7en"); 263 r.setDate( new Date () ); 264 MonetaryAmount amount = new MonetaryAmount( 265 new BigDecimal (100000), 266 Currency.getInstance("EUR") 267 ); 268 r.setAmount(amount); 269 s.persist(r); 270 tx.commit(); 271 s.clear(); 272 tx = s.beginTransaction(); 273 r = (Ransom) s.get( Ransom.class, r.getId() ); 274 assertNotNull(r); 275 assertNotNull( r.getAmount() ); 276 assertTrue( 0 == new BigDecimal (100000).compareTo( r.getAmount().getAmount() ) ); 277 assertEquals( Currency.getInstance("EUR"), r.getAmount().getCurrency() ); 278 tx.commit(); 279 s.close(); 280 } 281 282 public BasicHibernateAnnotationsTest(String x) { 283 super(x); 284 } 285 286 protected Class [] getMappings() { 287 return new Class [] { 288 Forest.class, 289 Tree.class, 290 Ransom.class, 291 ZipCode.class 292 }; 293 } 294 295 protected String [] getAnnotatedPackages() { 296 return new String [] { 297 "org.hibernate.test.annotations.entity" 298 }; 299 } 300 301 302 } 303 | Popular Tags |