1 package org.hibernate.persister; 3 4 import java.lang.reflect.Constructor ; 5 import java.lang.reflect.InvocationTargetException ; 6 7 import org.hibernate.HibernateException; 8 import org.hibernate.MappingException; 9 import org.hibernate.cache.CacheConcurrencyStrategy; 10 import org.hibernate.cfg.Configuration; 11 import org.hibernate.engine.Mapping; 12 import org.hibernate.engine.SessionFactoryImplementor; 13 import org.hibernate.mapping.Collection; 14 import org.hibernate.mapping.PersistentClass; 15 import org.hibernate.persister.collection.BasicCollectionPersister; 16 import org.hibernate.persister.collection.CollectionPersister; 17 import org.hibernate.persister.collection.OneToManyPersister; 18 import org.hibernate.persister.entity.EntityPersister; 19 import org.hibernate.persister.entity.JoinedSubclassEntityPersister; 20 import org.hibernate.persister.entity.SingleTableEntityPersister; 21 import org.hibernate.persister.entity.UnionSubclassEntityPersister; 22 23 28 public final class PersisterFactory { 29 30 33 35 private PersisterFactory() {} 36 37 private static final Class [] PERSISTER_CONSTRUCTOR_ARGS = new Class [] { 38 PersistentClass.class, CacheConcurrencyStrategy.class, SessionFactoryImplementor.class, Mapping.class 39 }; 40 41 private static final Class [] COLLECTION_PERSISTER_CONSTRUCTOR_ARGS = new Class [] { 44 Collection.class, CacheConcurrencyStrategy.class, Configuration.class, SessionFactoryImplementor.class 45 }; 46 47 public static EntityPersister createClassPersister( 48 PersistentClass model, 49 CacheConcurrencyStrategy cache, 50 SessionFactoryImplementor factory, 51 Mapping cfg) 52 throws HibernateException { 53 Class persisterClass = model.getEntityPersisterClass(); 54 if (persisterClass==null || persisterClass==SingleTableEntityPersister.class) { 55 return new SingleTableEntityPersister(model, cache, factory, cfg); 56 } 57 else if (persisterClass==JoinedSubclassEntityPersister.class) { 58 return new JoinedSubclassEntityPersister(model, cache, factory, cfg); 59 } 60 else if (persisterClass==UnionSubclassEntityPersister.class) { 61 return new UnionSubclassEntityPersister(model, cache, factory, cfg); 62 } 63 else { 64 return create(persisterClass, model, cache, factory, cfg); 65 } 66 } 67 68 public static CollectionPersister createCollectionPersister(Configuration cfg, Collection model, CacheConcurrencyStrategy cache, SessionFactoryImplementor factory) 69 throws HibernateException { 70 Class persisterClass = model.getCollectionPersisterClass(); 71 if(persisterClass==null) { return model.isOneToMany() ? 73 (CollectionPersister) new OneToManyPersister(model, cache, cfg, factory) : 74 (CollectionPersister) new BasicCollectionPersister(model, cache, cfg, factory); 75 } 76 else { 77 return create(persisterClass, cfg, model, cache, factory); 78 } 79 80 } 81 82 private static EntityPersister create( 83 Class persisterClass, 84 PersistentClass model, 85 CacheConcurrencyStrategy cache, 86 SessionFactoryImplementor factory, 87 Mapping cfg) 88 throws HibernateException { 89 Constructor pc; 90 try { 91 pc = persisterClass.getConstructor(PERSISTER_CONSTRUCTOR_ARGS); 92 } 93 catch (Exception e) { 94 throw new MappingException( "Could not get constructor for " + persisterClass.getName(), e ); 95 } 96 97 try { 98 return (EntityPersister) pc.newInstance( new Object [] { model, cache, factory, cfg } ); 99 } 100 catch (InvocationTargetException ite) { 101 Throwable e = ite.getTargetException(); 102 if (e instanceof HibernateException) { 103 throw (HibernateException) e; 104 } 105 else { 106 throw new MappingException( "Could not instantiate persister " + persisterClass.getName(), e ); 107 } 108 } 109 catch (Exception e) { 110 throw new MappingException( "Could not instantiate persister " + persisterClass.getName(), e ); 111 } 112 } 113 114 private static CollectionPersister create(Class persisterClass, Configuration cfg, Collection model, CacheConcurrencyStrategy cache, SessionFactoryImplementor factory) throws HibernateException { 115 Constructor pc; 116 try { 117 pc = persisterClass.getConstructor(COLLECTION_PERSISTER_CONSTRUCTOR_ARGS); 118 } 119 catch (Exception e) { 120 throw new MappingException( "Could not get constructor for " + persisterClass.getName(), e ); 121 } 122 123 try { 124 return (CollectionPersister) pc.newInstance( new Object [] { model, cache, cfg, factory } ); 125 } 126 catch (InvocationTargetException ite) { 127 Throwable e = ite.getTargetException(); 128 if (e instanceof HibernateException) { 129 throw (HibernateException) e; 130 } 131 else { 132 throw new MappingException( "Could not instantiate collection persister " + persisterClass.getName(), e ); 133 } 134 } 135 catch (Exception e) { 136 throw new MappingException( "Could not instantiate collection persister " + persisterClass.getName(), e ); 137 } 138 } 139 140 141 } 142 | Popular Tags |