1 package org.hibernate.cfg; 3 4 import java.util.ArrayList ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.Properties ; 8 import javax.persistence.Embeddable; 9 import javax.persistence.Entity; 10 import javax.persistence.EmbeddableSuperclass; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.hibernate.MappingException; 15 import org.hibernate.mapping.IdGenerator; 16 import org.hibernate.mapping.Join; 17 import org.hibernate.mapping.PersistentClass; 18 import org.hibernate.mapping.Table; 19 20 27 public class ExtendedMappings extends Mappings { 28 29 private static final Log log = LogFactory.getLog(ExtendedMappings.class); 30 31 private final Map <String , IdGenerator> namedGenerators; 32 private final Map <String , Map <String , Join>> joins; 33 private final Map <Class , AnnotatedClassType> classTypes; 34 private final Map <String , Properties > generatorTables; 35 private final Map <Table, List <String []>> tableUniqueConstraints; 36 private final Map <String , String > mappedByResolver; 37 private final Map <String , String > propertyRefResolver; 38 39 ExtendedMappings( 40 Map classes, Map collections, Map tables, Map queries, Map sqlqueries, Map sqlResultSetMappings, 41 Map imports, 42 List secondPasses, List propertyReferences, NamingStrategy namingStrategy, Map typeDefs, 43 Map filterDefinitions, Map namedGenerators, Map <String , Map <String , Join>> joins, Map <Class , 44 AnnotatedClassType> classTypes, Map extendsQueue, Map <String , Properties > generatorTables, 45 Map <Table, List <String []>> tableUniqueConstraints, Map <String , String > mappedByResolver, 46 Map <String , String > propertyRefResolver 47 ) { 48 super( 49 classes, 50 collections, 51 tables, 52 queries, 53 sqlqueries, 54 sqlResultSetMappings, 55 imports, 56 secondPasses, 57 propertyReferences, 58 namingStrategy, 59 typeDefs, 60 filterDefinitions, 61 extendsQueue); 62 this.namedGenerators = namedGenerators; 63 this.joins = joins; 64 this.classTypes = classTypes; 65 this.generatorTables = generatorTables; 66 this.tableUniqueConstraints = tableUniqueConstraints; 67 this.mappedByResolver = mappedByResolver; 68 this.propertyRefResolver = propertyRefResolver; 69 } 70 71 public void addGenerator(IdGenerator generator) throws MappingException { 72 Object old = namedGenerators.put( generator.getName(), generator ); 73 if ( old!=null ) log.warn( "duplicate generator name: " + generator.getName() ); 74 } 75 76 public void addJoins(PersistentClass persistentClass, Map <String , Join> joins) throws MappingException { 77 Object old = this.joins.put( persistentClass.getEntityName(), joins ); 78 if ( old!=null ) log.warn( "duplicate joins for class: " + persistentClass.getEntityName() ); 79 } 80 81 public AnnotatedClassType addClassType(Class clazz) { 82 AnnotatedClassType type; 83 if ( clazz.isAnnotationPresent(Entity.class) ) { 84 type = AnnotatedClassType.ENTITY; 85 } 86 else if (clazz.isAnnotationPresent(Embeddable.class) ) { 87 type = AnnotatedClassType.EMBEDDABLE; 88 } 89 else if (clazz.isAnnotationPresent(EmbeddableSuperclass.class) ) { 90 type = AnnotatedClassType.EMBEDDABLE_SUPERCLASS; 91 } 92 else { 93 type = AnnotatedClassType.NONE; 94 } 95 classTypes.put(clazz, type); 96 return type; 97 } 98 99 103 public AnnotatedClassType getClassType(Class clazz) { 104 AnnotatedClassType type = classTypes.get(clazz); 105 if (type == null) { 106 return addClassType(clazz); 107 } 108 else { 109 return type; 110 } 111 } 112 113 public IdGenerator getGenerator(String name) { 114 return getGenerator(name, null); 115 } 116 117 public Map <String , Join> getJoins(String persistentClass) { 118 return joins.get(persistentClass); 119 } 120 121 129 public IdGenerator getGenerator(String name, Map <String , IdGenerator> localGenerators) { 130 if (localGenerators != null) { 131 IdGenerator result = localGenerators.get(name); 132 if (result != null) return result; 133 } 134 return namedGenerators.get(name); 135 } 136 137 public void addGeneratorTable(String name, Properties params) { 138 Object old = generatorTables.put(name, params); 139 if ( old!=null ) log.warn( "duplicate generator table: " + name); 140 } 141 142 public Properties getGeneratorTableProperties(String name, Map <String , Properties > localGeneratorTables) { 143 if (localGeneratorTables != null) { 144 Properties result = localGeneratorTables.get(name); 145 if (result != null) return result; 146 } 147 return generatorTables.get(name); 148 } 149 150 public void addUniqueConstraints(Table table, List uniqueConstraints) { 151 List oldConstraints = tableUniqueConstraints.get(table); 152 if (oldConstraints == null) { 153 oldConstraints = new ArrayList (); 154 tableUniqueConstraints.put(table, oldConstraints); 155 } 156 oldConstraints.addAll(uniqueConstraints); 157 } 158 159 public Map <Table, List <String []>> getTableUniqueConstraints() { 160 return tableUniqueConstraints; 161 } 162 163 public void addMappedBy(String entityName, String propertyName, String inversePropertyName) { 164 mappedByResolver.put( entityName + "." + propertyName, inversePropertyName ); 165 } 166 167 public String getFromMappedBy(String entityName, String propertyName) { 168 return mappedByResolver.get(entityName + "." + propertyName); 169 } 170 171 public void addPropertyReferencedAssociation(String entityName, String propertyName, String propertyRef) { 172 propertyRefResolver.put( entityName + "." + propertyName, propertyRef ); 173 } 174 175 public String getPropertyReferencedAssociation(String entityName, String propertyName) { 176 return propertyRefResolver.get(entityName + "." + propertyName); 177 } 178 179 @Override public void addUniquePropertyReference(String referencedClass, String propertyName) { 180 super.addUniquePropertyReference( referencedClass, propertyName ); 181 } 182 183 @Override public void addPropertyReference(String referencedClass, String propertyName) { 184 super.addPropertyReference( referencedClass, propertyName ); 185 } 186 } 187 | Popular Tags |