KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > CustomPersister


1 //$Id: CustomPersister.java,v 1.22 2005/07/16 22:28:30 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Map JavaDoc;
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 JavaDoc INSTANCES = new Hashtable JavaDoc();
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 JavaDoc( "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 JavaDoc getMappedClass() {
66         return Custom.class;
67     }
68
69     public void postInstantiate() throws MappingException {}
70
71     public String JavaDoc getEntityName() {
72         return Custom.class.getName();
73     }
74     
75     public boolean isSubclassEntityName(String JavaDoc 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 JavaDoc isTransient(Object JavaDoc object, SessionImplementor session) {
104         return new Boolean JavaDoc( ( (Custom) object ).id==null );
105     }
106
107     public Object JavaDoc[] getPropertyValuesToInsert(Object JavaDoc object, Map JavaDoc mergeMap, SessionImplementor session)
108     throws HibernateException {
109         return getPropertyValues( object, session.getEntityMode() );
110     }
111
112     public Class JavaDoc 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 JavaDoc getConcreteProxyClass(EntityMode entityMode) {
128         checkEntityMode( entityMode );
129         return Custom.class;
130     }
131
132     public void setPropertyValues(Object JavaDoc object, Object JavaDoc[] values, EntityMode entityMode) throws HibernateException {
133         checkEntityMode( entityMode );
134         setPropertyValue( object, 0, values[0], entityMode );
135     }
136
137     public void setPropertyValue(Object JavaDoc object, int i, Object JavaDoc value, EntityMode entityMode) throws HibernateException {
138         checkEntityMode( entityMode );
139         ( (Custom) object ).setName( (String JavaDoc) value );
140     }
141
142     public Object JavaDoc[] getPropertyValues(Object JavaDoc object, EntityMode entityMode) throws HibernateException {
143         checkEntityMode( entityMode );
144         Custom c = (Custom) object;
145         return new Object JavaDoc[] { c.getName() };
146     }
147
148     public Object JavaDoc getPropertyValue(Object JavaDoc object, int i, EntityMode entityMode) throws HibernateException {
149         checkEntityMode( entityMode );
150         return ( (Custom) object ).getName();
151     }
152
153     public Object JavaDoc getPropertyValue(Object JavaDoc object, String JavaDoc propertyName, EntityMode entityMode) throws HibernateException {
154         checkEntityMode( entityMode );
155         return ( (Custom) object ).getName();
156     }
157
158     public Serializable JavaDoc getIdentifier(Object JavaDoc object, EntityMode entityMode) throws HibernateException {
159         checkEntityMode( entityMode );
160         return ( (Custom) object ).id;
161     }
162
163     public void setIdentifier(Object JavaDoc object, Serializable JavaDoc id, EntityMode entityMode) throws HibernateException {
164         checkEntityMode( entityMode );
165         ( (Custom) object ).id = (String JavaDoc) id;
166     }
167
168     public Object JavaDoc getVersion(Object JavaDoc object, EntityMode entityMode) throws HibernateException {
169         checkEntityMode( entityMode );
170         return null;
171     }
172
173     public Object JavaDoc instantiate(Serializable JavaDoc id, EntityMode entityMode) throws HibernateException {
174         checkEntityMode( entityMode );
175         Custom c = new Custom();
176         c.id = (String JavaDoc) id;
177         return c;
178     }
179
180     public boolean isInstance(Object JavaDoc object, EntityMode entityMode) {
181         checkEntityMode( entityMode );
182         return object instanceof Custom;
183     }
184
185     public boolean hasUninitializedLazyProperties(Object JavaDoc object, EntityMode entityMode) {
186         checkEntityMode( entityMode );
187         return false;
188     }
189
190     public void resetIdentifier(Object JavaDoc entity, Serializable JavaDoc currentId, Object JavaDoc currentVersion, EntityMode entityMode) {
191         checkEntityMode( entityMode );
192         ( ( Custom ) entity ).id = ( String JavaDoc ) currentId;
193     }
194
195     public EntityPersister getSubclassEntityPersister(Object JavaDoc instance, SessionFactoryImplementor factory, EntityMode entityMode) {
196         checkEntityMode( entityMode );
197         return this;
198     }
199
200     public int[] findDirty(
201         Object JavaDoc[] x,
202         Object JavaDoc[] y,
203         Object JavaDoc 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 JavaDoc[] x,
216         Object JavaDoc[] y,
217         Object JavaDoc 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     /**
229      * @see EntityPersister#hasIdentifierProperty()
230      */

231     public boolean hasIdentifierProperty() {
232         return true;
233     }
234
235
236     /**
237      * @see EntityPersister#isVersioned()
238      */

239     public boolean isVersioned() {
240         return false;
241     }
242
243     /**
244      * @see EntityPersister#getVersionType()
245      */

246     public VersionType getVersionType() {
247         return null;
248     }
249
250     /**
251      * @see EntityPersister#getVersionProperty()
252      */

253     public int getVersionProperty() {
254         return 0;
255     }
256
257     /**
258      * @see EntityPersister#getIdentifierGenerator()
259      */

260     public IdentifierGenerator getIdentifierGenerator()
261     throws HibernateException {
262         return GENERATOR;
263     }
264
265     /**
266      * @see EntityPersister#load(Serializable, Object, LockMode, SessionImplementor)
267      */

268     public Object JavaDoc load(
269         Serializable JavaDoc id,
270         Object JavaDoc optionalObject,
271         LockMode lockMode,
272         SessionImplementor session
273     ) throws HibernateException {
274
275         // fails when optional object is supplied
276

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 JavaDoc[] { 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     /**
310      * @see EntityPersister#lock(Serializable, Object, Object, LockMode, SessionImplementor)
311      */

312     public void lock(
313         Serializable JavaDoc id,
314         Object JavaDoc version,
315         Object JavaDoc object,
316         LockMode lockMode,
317         SessionImplementor session
318     ) throws HibernateException {
319
320         throw new UnsupportedOperationException JavaDoc();
321     }
322
323     public void insert(
324         Serializable JavaDoc id,
325         Object JavaDoc[] fields,
326         Object JavaDoc object,
327         SessionImplementor session
328     ) throws HibernateException {
329
330         INSTANCES.put(id, ( (Custom) object ).clone() );
331     }
332
333     public Serializable JavaDoc insert(Object JavaDoc[] fields, Object JavaDoc object, SessionImplementor session)
334     throws HibernateException {
335
336         throw new UnsupportedOperationException JavaDoc();
337     }
338
339     public void delete(
340         Serializable JavaDoc id,
341         Object JavaDoc version,
342         Object JavaDoc object,
343         SessionImplementor session
344     ) throws HibernateException {
345
346         INSTANCES.remove(id);
347     }
348
349     /**
350      * @see EntityPersister
351      */

352     public void update(
353         Serializable JavaDoc id,
354         Object JavaDoc[] fields,
355         int[] dirtyFields,
356         boolean hasDirtyCollection,
357         Object JavaDoc[] oldFields,
358         Object JavaDoc oldVersion,
359         Object JavaDoc object,
360         Object JavaDoc 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 JavaDoc[] NAMES = new String JavaDoc[] { "name" };
370     private static final boolean[] MUTABILITY = new boolean[] { true };
371
372     /**
373      * @see EntityPersister#getPropertyTypes()
374      */

375     public Type[] getPropertyTypes() {
376         return TYPES;
377     }
378
379     /**
380      * @see EntityPersister#getPropertyNames()
381      */

382     public String JavaDoc[] getPropertyNames() {
383         return NAMES;
384     }
385
386     /**
387      * @see EntityPersister#getPropertyCascadeStyles()
388      */

389     public CascadeStyle[] getPropertyCascadeStyles() {
390         return null;
391     }
392
393     /**
394      * @see EntityPersister#getIdentifierType()
395      */

396     public Type getIdentifierType() {
397         return Hibernate.LONG;
398     }
399
400     /**
401      * @see EntityPersister#getIdentifierPropertyName()
402      */

403     public String JavaDoc getIdentifierPropertyName() {
404         return "id";
405     }
406
407     /**
408      * @see EntityPersister#hasCache()
409      */

410     public boolean hasCache() {
411         return false;
412     }
413
414     /**
415      * @see EntityPersister#getCache()
416      */

417     public CacheConcurrencyStrategy getCache() {
418         return null;
419     }
420
421     /**
422      * @see EntityPersister#getRootEntityName()
423      */

424     public String JavaDoc getRootEntityName() {
425         return "CUSTOMS";
426     }
427
428     public Serializable JavaDoc[] getPropertySpaces() {
429         return new String JavaDoc[] { "CUSTOMS" };
430     }
431
432     public Serializable JavaDoc[] getQuerySpaces() {
433         return new String JavaDoc[] { "CUSTOMS" };
434     }
435
436     /**
437      * @see EntityPersister#getClassMetadata()
438      */

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     /**
452      * @see EntityPersister#getPropertyInsertability()
453      */

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 JavaDoc propertyName) {
467         throw new UnsupportedOperationException JavaDoc();
468     }
469
470     public Object JavaDoc getPropertyValue(Object JavaDoc object, String JavaDoc propertyName)
471         throws HibernateException {
472         throw new UnsupportedOperationException JavaDoc();
473     }
474
475     public Object JavaDoc createProxy(Serializable JavaDoc id, SessionImplementor session)
476         throws HibernateException {
477         throw new UnsupportedOperationException JavaDoc("no proxy for this class");
478     }
479
480     public Object JavaDoc getCurrentVersion(
481         Serializable JavaDoc id,
482         SessionImplementor session)
483         throws HibernateException {
484
485         return INSTANCES.get(id);
486     }
487
488     public EntityMode guessEntityMode(Object JavaDoc 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 JavaDoc alias, Map JavaDoc filters) {
510     }
511
512     public void applyFilters(Select select, String JavaDoc alias, Map JavaDoc filters) {
513     }
514     
515     
516     public void afterInitialize(Object JavaDoc entity, boolean fetched, SessionImplementor session) {
517     }
518
519     public void afterReassociate(Object JavaDoc entity, SessionImplementor session) {
520     }
521
522     public Object JavaDoc[] getDatabaseSnapshot(Serializable JavaDoc 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