1 package org.hibernate.test.legacy; 3 4 import java.io.Serializable ; 5 import java.util.Hashtable ; 6 import java.util.Map ; 7 8 import org.hibernate.EntityMode; 9 import org.hibernate.Hibernate; 10 import org.hibernate.HibernateException; 11 import org.hibernate.LockMode; 12 import org.hibernate.MappingException; 13 import org.hibernate.cache.CacheConcurrencyStrategy; 14 import org.hibernate.cache.entry.CacheEntryStructure; 15 import org.hibernate.cache.entry.UnstructuredCacheEntry; 16 import org.hibernate.engine.CascadeStyle; 17 import org.hibernate.engine.EntityKey; 18 import org.hibernate.engine.Mapping; 19 import org.hibernate.engine.SessionFactoryImplementor; 20 import org.hibernate.engine.SessionImplementor; 21 import org.hibernate.engine.TwoPhaseLoad; 22 import org.hibernate.event.EventSource; 23 import org.hibernate.event.PostLoadEvent; 24 import org.hibernate.event.PreLoadEvent; 25 import org.hibernate.id.IdentifierGenerator; 26 import org.hibernate.id.UUIDHexGenerator; 27 import org.hibernate.mapping.PersistentClass; 28 import org.hibernate.metadata.ClassMetadata; 29 import org.hibernate.persister.entity.EntityPersister; 30 import org.hibernate.sql.QuerySelect; 31 import org.hibernate.sql.Select; 32 import org.hibernate.type.Type; 33 import org.hibernate.type.VersionType; 34 import org.hibernate.util.EqualsHelper; 35 36 public class CustomPersister implements EntityPersister { 37 38 private static final Hashtable INSTANCES = new Hashtable (); 39 private static final IdentifierGenerator GENERATOR = new UUIDHexGenerator(); 40 41 private SessionFactoryImplementor factory; 42 43 public CustomPersister( 44 PersistentClass model, 45 CacheConcurrencyStrategy cache, 46 SessionFactoryImplementor factory, 47 Mapping mapping) { 48 this.factory = factory; 49 } 50 51 private void checkEntityMode(EntityMode entityMode) { 52 if ( EntityMode.POJO != entityMode ) { 53 throw new IllegalArgumentException ( "Unhandled EntityMode : " + entityMode ); 54 } 55 } 56 57 public boolean isInherited() { 58 return false; 59 } 60 61 public SessionFactoryImplementor getFactory() { 62 return factory; 63 } 64 65 public Class getMappedClass() { 66 return Custom.class; 67 } 68 69 public void postInstantiate() throws MappingException {} 70 71 public String getEntityName() { 72 return Custom.class.getName(); 73 } 74 75 public boolean isSubclassEntityName(String entityName) { 76 return Custom.class.getName().equals(entityName); 77 } 78 79 public boolean hasProxy() { 80 return false; 81 } 82 83 public boolean hasCollections() { 84 return false; 85 } 86 87 public boolean hasCascades() { 88 return false; 89 } 90 91 public boolean isMutable() { 92 return true; 93 } 94 95 public boolean isSelectBeforeUpdateRequired() { 96 return false; 97 } 98 99 public boolean isIdentifierAssignedByInsert() { 100 return false; 101 } 102 103 public Boolean isTransient(Object object, SessionImplementor session) { 104 return new Boolean ( ( (Custom) object ).id==null ); 105 } 106 107 public Object [] getPropertyValuesToInsert(Object object, Map mergeMap, SessionImplementor session) 108 throws HibernateException { 109 return getPropertyValues( object, session.getEntityMode() ); 110 } 111 112 public Class getMappedClass(EntityMode entityMode) { 113 checkEntityMode( entityMode ); 114 return Custom.class; 115 } 116 117 public boolean implementsLifecycle(EntityMode entityMode) { 118 checkEntityMode( entityMode ); 119 return false; 120 } 121 122 public boolean implementsValidatable(EntityMode entityMode) { 123 checkEntityMode( entityMode ); 124 return false; 125 } 126 127 public Class getConcreteProxyClass(EntityMode entityMode) { 128 checkEntityMode( entityMode ); 129 return Custom.class; 130 } 131 132 public void setPropertyValues(Object object, Object [] values, EntityMode entityMode) throws HibernateException { 133 checkEntityMode( entityMode ); 134 setPropertyValue( object, 0, values[0], entityMode ); 135 } 136 137 public void setPropertyValue(Object object, int i, Object value, EntityMode entityMode) throws HibernateException { 138 checkEntityMode( entityMode ); 139 ( (Custom) object ).setName( (String ) value ); 140 } 141 142 public Object [] getPropertyValues(Object object, EntityMode entityMode) throws HibernateException { 143 checkEntityMode( entityMode ); 144 Custom c = (Custom) object; 145 return new Object [] { c.getName() }; 146 } 147 148 public Object getPropertyValue(Object object, int i, EntityMode entityMode) throws HibernateException { 149 checkEntityMode( entityMode ); 150 return ( (Custom) object ).getName(); 151 } 152 153 public Object getPropertyValue(Object object, String propertyName, EntityMode entityMode) throws HibernateException { 154 checkEntityMode( entityMode ); 155 return ( (Custom) object ).getName(); 156 } 157 158 public Serializable getIdentifier(Object object, EntityMode entityMode) throws HibernateException { 159 checkEntityMode( entityMode ); 160 return ( (Custom) object ).id; 161 } 162 163 public void setIdentifier(Object object, Serializable id, EntityMode entityMode) throws HibernateException { 164 checkEntityMode( entityMode ); 165 ( (Custom) object ).id = (String ) id; 166 } 167 168 public Object getVersion(Object object, EntityMode entityMode) throws HibernateException { 169 checkEntityMode( entityMode ); 170 return null; 171 } 172 173 public Object instantiate(Serializable id, EntityMode entityMode) throws HibernateException { 174 checkEntityMode( entityMode ); 175 Custom c = new Custom(); 176 c.id = (String ) id; 177 return c; 178 } 179 180 public boolean isInstance(Object object, EntityMode entityMode) { 181 checkEntityMode( entityMode ); 182 return object instanceof Custom; 183 } 184 185 public boolean hasUninitializedLazyProperties(Object object, EntityMode entityMode) { 186 checkEntityMode( entityMode ); 187 return false; 188 } 189 190 public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion, EntityMode entityMode) { 191 checkEntityMode( entityMode ); 192 ( ( Custom ) entity ).id = ( String ) currentId; 193 } 194 195 public EntityPersister getSubclassEntityPersister(Object instance, SessionFactoryImplementor factory, EntityMode entityMode) { 196 checkEntityMode( entityMode ); 197 return this; 198 } 199 200 public int[] findDirty( 201 Object [] x, 202 Object [] y, 203 Object owner, 204 SessionImplementor session 205 ) throws HibernateException { 206 if ( !EqualsHelper.equals( x[0], y[0] ) ) { 207 return new int[] { 0 }; 208 } 209 else { 210 return null; 211 } 212 } 213 214 public int[] findModified( 215 Object [] x, 216 Object [] y, 217 Object owner, 218 SessionImplementor session 219 ) throws HibernateException { 220 if ( !EqualsHelper.equals( x[0], y[0] ) ) { 221 return new int[] { 0 }; 222 } 223 else { 224 return null; 225 } 226 } 227 228 231 public boolean hasIdentifierProperty() { 232 return true; 233 } 234 235 236 239 public boolean isVersioned() { 240 return false; 241 } 242 243 246 public VersionType getVersionType() { 247 return null; 248 } 249 250 253 public int getVersionProperty() { 254 return 0; 255 } 256 257 260 public IdentifierGenerator getIdentifierGenerator() 261 throws HibernateException { 262 return GENERATOR; 263 } 264 265 268 public Object load( 269 Serializable id, 270 Object optionalObject, 271 LockMode lockMode, 272 SessionImplementor session 273 ) throws HibernateException { 274 275 277 Custom clone = null; 278 Custom obj = (Custom) INSTANCES.get(id); 279 if (obj!=null) { 280 clone = (Custom) obj.clone(); 281 TwoPhaseLoad.addUninitializedEntity( 282 new EntityKey( id, this, session.getEntityMode() ), 283 clone, 284 this, 285 LockMode.NONE, 286 false, 287 session 288 ); 289 TwoPhaseLoad.postHydrate( 290 this, id, 291 new String [] { obj.getName() }, 292 null, 293 clone, 294 LockMode.NONE, 295 false, 296 session 297 ); 298 TwoPhaseLoad.initializeEntity( 299 clone, 300 false, 301 session, 302 new PreLoadEvent( (EventSource) session ), 303 new PostLoadEvent( (EventSource) session ) 304 ); 305 } 306 return clone; 307 } 308 309 312 public void lock( 313 Serializable id, 314 Object version, 315 Object object, 316 LockMode lockMode, 317 SessionImplementor session 318 ) throws HibernateException { 319 320 throw new UnsupportedOperationException (); 321 } 322 323 public void insert( 324 Serializable id, 325 Object [] fields, 326 Object object, 327 SessionImplementor session 328 ) throws HibernateException { 329 330 INSTANCES.put(id, ( (Custom) object ).clone() ); 331 } 332 333 public Serializable insert(Object [] fields, Object object, SessionImplementor session) 334 throws HibernateException { 335 336 throw new UnsupportedOperationException (); 337 } 338 339 public void delete( 340 Serializable id, 341 Object version, 342 Object object, 343 SessionImplementor session 344 ) throws HibernateException { 345 346 INSTANCES.remove(id); 347 } 348 349 352 public void update( 353 Serializable id, 354 Object [] fields, 355 int[] dirtyFields, 356 boolean hasDirtyCollection, 357 Object [] oldFields, 358 Object oldVersion, 359 Object object, 360 Object rowId, 361 SessionImplementor session 362 ) throws HibernateException { 363 364 INSTANCES.put( id, ( (Custom) object ).clone() ); 365 366 } 367 368 private static final Type[] TYPES = new Type[] { Hibernate.STRING }; 369 private static final String [] NAMES = new String [] { "name" }; 370 private static final boolean[] MUTABILITY = new boolean[] { true }; 371 372 375 public Type[] getPropertyTypes() { 376 return TYPES; 377 } 378 379 382 public String [] getPropertyNames() { 383 return NAMES; 384 } 385 386 389 public CascadeStyle[] getPropertyCascadeStyles() { 390 return null; 391 } 392 393 396 public Type getIdentifierType() { 397 return Hibernate.LONG; 398 } 399 400 403 public String getIdentifierPropertyName() { 404 return "id"; 405 } 406 407 410 public boolean hasCache() { 411 return false; 412 } 413 414 417 public CacheConcurrencyStrategy getCache() { 418 return null; 419 } 420 421 424 public String getRootEntityName() { 425 return "CUSTOMS"; 426 } 427 428 public Serializable [] getPropertySpaces() { 429 return new String [] { "CUSTOMS" }; 430 } 431 432 public Serializable [] getQuerySpaces() { 433 return new String [] { "CUSTOMS" }; 434 } 435 436 439 public ClassMetadata getClassMetadata() { 440 return null; 441 } 442 443 public boolean[] getPropertyUpdateability() { 444 return MUTABILITY; 445 } 446 447 public boolean[] getPropertyCheckability() { 448 return MUTABILITY; 449 } 450 451 454 public boolean[] getPropertyInsertability() { 455 return MUTABILITY; 456 } 457 458 public boolean hasIdentifierPropertyOrEmbeddedCompositeIdentifier() { 459 return true; 460 } 461 462 public boolean isBatchLoadable() { 463 return false; 464 } 465 466 public Type getPropertyType(String propertyName) { 467 throw new UnsupportedOperationException (); 468 } 469 470 public Object getPropertyValue(Object object, String propertyName) 471 throws HibernateException { 472 throw new UnsupportedOperationException (); 473 } 474 475 public Object createProxy(Serializable id, SessionImplementor session) 476 throws HibernateException { 477 throw new UnsupportedOperationException ("no proxy for this class"); 478 } 479 480 public Object getCurrentVersion( 481 Serializable id, 482 SessionImplementor session) 483 throws HibernateException { 484 485 return INSTANCES.get(id); 486 } 487 488 public EntityMode guessEntityMode(Object object) { 489 if ( !isInstance(object, EntityMode.POJO) ) { 490 return null; 491 } 492 else { 493 return EntityMode.POJO; 494 } 495 } 496 497 public boolean[] getPropertyNullability() { 498 return MUTABILITY; 499 } 500 501 public boolean isDynamic() { 502 return false; 503 } 504 505 public boolean isCacheInvalidationRequired() { 506 return false; 507 } 508 509 public void applyFilters(QuerySelect select, String alias, Map filters) { 510 } 511 512 public void applyFilters(Select select, String alias, Map filters) { 513 } 514 515 516 public void afterInitialize(Object entity, boolean fetched, SessionImplementor session) { 517 } 518 519 public void afterReassociate(Object entity, SessionImplementor session) { 520 } 521 522 public Object [] getDatabaseSnapshot(Serializable id, SessionImplementor session) 523 throws HibernateException { 524 return null; 525 } 526 527 public boolean[] getPropertyVersionability() { 528 return MUTABILITY; 529 } 530 531 public CacheEntryStructure getCacheEntryStructure() { 532 return new UnstructuredCacheEntry(); 533 } 534 535 public boolean hasSubselectLoadableCollections() { 536 return false; 537 } 538 539 public int[] getNaturalIdentifierProperties() { 540 return null; 541 } 542 543 public Type[] getNaturalIdentifierTypes() { 544 return null; 545 } 546 547 public boolean hasNaturalIdentifier() { 548 return false; 549 } 550 551 public boolean hasMutableProperties() { 552 return false; 553 } 554 555 public boolean isInstrumented(EntityMode entityMode) { 556 return false; 557 } 558 559 } 560 561 562 563 564 565 566 | Popular Tags |