1 package org.hibernate.cfg; 3 4 import java.io.Serializable ; 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Properties ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.dom4j.Document; 14 import org.hibernate.MappingException; 15 import org.hibernate.engine.FilterDefinition; 16 import org.hibernate.engine.NamedQueryDefinition; 17 import org.hibernate.engine.NamedSQLQueryDefinition; 18 import org.hibernate.engine.ResultSetMappingDefinition; 19 import org.hibernate.mapping.Collection; 20 import org.hibernate.mapping.DenormalizedTable; 21 import org.hibernate.mapping.PersistentClass; 22 import org.hibernate.mapping.Table; 23 import org.hibernate.mapping.TypeDef; 24 import org.hibernate.util.StringHelper; 25 26 32 public class Mappings implements Serializable { 33 34 private static final Log log = LogFactory.getLog(Mappings.class); 35 36 private final Map classes; 37 private final Map collections; 38 private final Map tables; 39 private final Map queries; 40 private final Map sqlqueries; 41 private final Map resultSetMappings; 42 private final Map typeDefs; 43 private final List secondPasses; 44 private final Map imports; 45 private String schemaName; 46 private String catalogName; 47 private String defaultCascade; 48 private String defaultPackage; 49 private String defaultAccess; 50 private boolean autoImport; 51 private boolean defaultLazy; 52 private final List propertyReferences; 53 private final NamingStrategy namingStrategy; 54 private final Map filterDefinitions; 55 56 private final Map extendsQueue; 57 58 59 Mappings( 60 final Map classes, 61 final Map collections, 62 final Map tables, 63 final Map queries, 64 final Map sqlqueries, 65 final Map sqlResultSetMappings, 66 final Map imports, 67 final List secondPasses, 68 final List propertyReferences, 69 final NamingStrategy namingStrategy, 70 final Map typeDefs, 71 final Map filterDefinitions, 72 final Map extendsQueue 73 ) { 74 this.classes = classes; 75 this.collections = collections; 76 this.queries = queries; 77 this.sqlqueries = sqlqueries; 78 this.resultSetMappings = sqlResultSetMappings; 79 this.tables = tables; 80 this.imports = imports; 81 this.secondPasses = secondPasses; 82 this.propertyReferences = propertyReferences; 83 this.namingStrategy = namingStrategy; 84 this.typeDefs = typeDefs; 85 this.filterDefinitions = filterDefinitions; 86 this.extendsQueue = extendsQueue; 87 } 88 89 public void addClass(PersistentClass persistentClass) throws MappingException { 90 Object old = classes.put( persistentClass.getEntityName(), persistentClass ); 91 if ( old!=null ) { 92 throw new MappingException( "duplicate class mapping: " + persistentClass.getEntityName() ); 93 } 94 } 95 public void addCollection(Collection collection) throws MappingException { 96 Object old = collections.put( collection.getRole(), collection ); 97 if ( old!=null ) { 98 throw new MappingException( "duplicate collection role mapping: " + collection.getRole() ); 99 } 100 } 101 public PersistentClass getClass(String className) { 102 return (PersistentClass) classes.get(className); 103 } 104 public Collection getCollection(String role) { 105 return (Collection) collections.get(role); 106 } 107 108 public void addImport(String className, String rename) throws MappingException { 109 String existing = (String ) imports.put(rename, className); 110 if ( existing!=null ) { 111 if ( existing.equals(className) ) { 112 log.info( "duplicate import: " + className + "->" + rename ); 113 } 114 else { 115 throw new MappingException( 116 "duplicate import: " + rename + 117 " refers to both " + className + 118 " and " + existing + 119 " (try using auto-import=\"false\")" 120 ); 121 } 122 } 123 } 124 125 public Table addTable(String schema, 126 String catalog, 127 String name, 128 String subselect, 129 boolean isAbstract 130 ) { 131 String key = subselect==null ? 132 Table.qualify(catalog, schema, name, '.') : 133 subselect; 134 Table table = (Table) tables.get(key); 135 136 if (table == null) { 137 table = new Table(); 138 table.setAbstract(isAbstract); 139 table.setName(name); 140 table.setSchema(schema); 141 table.setCatalog(catalog); 142 table.setSubselect(subselect); 143 tables.put(key, table); 144 } 145 else { 146 if (!isAbstract) table.setAbstract(false); 147 } 148 149 return table; 150 } 151 152 public Table addDenormalizedTable( 153 String schema, 154 String catalog, 155 String name, 156 boolean isAbstract, 157 String subselect, 158 Table includedTable) 159 throws MappingException { 160 String key = subselect==null ? 161 Table.qualify(catalog, schema, name, '.') : 162 subselect; 163 if ( tables.containsKey(key) ) { 164 throw new MappingException("duplicate table: " + name); 165 } 166 Table table = new DenormalizedTable(includedTable); 167 table.setAbstract(isAbstract); 168 table.setName(name); 169 table.setSchema(schema); 170 table.setCatalog(catalog); 171 table.setSubselect(subselect); 172 tables.put(key, table); 173 return table; 174 } 175 176 public Table getTable(String schema, String catalog, String name) { 177 String key = Table.qualify(catalog, schema, name, '.'); 178 return (Table) tables.get(key); 179 } 180 181 public String getSchemaName() { 182 return schemaName; 183 } 184 185 public String getCatalogName() { 186 return catalogName; 187 } 188 189 public String getDefaultCascade() { 190 return defaultCascade; 191 } 192 193 197 public void setSchemaName(String schemaName) { 198 this.schemaName = schemaName; 199 } 200 201 205 public void setCatalogName(String catalogName) { 206 this.catalogName = catalogName; 207 } 208 209 213 public void setDefaultCascade(String defaultCascade) { 214 this.defaultCascade = defaultCascade; 215 } 216 217 221 public void setDefaultAccess(String defaultAccess) { 222 this.defaultAccess = defaultAccess; 223 } 224 225 public String getDefaultAccess() { 226 return defaultAccess; 227 } 228 229 public void addQuery(String name, NamedQueryDefinition query) throws MappingException { 230 checkQueryExist(name); 231 queries.put( name.intern(), query ); 232 } 233 234 public void addSQLQuery(String name, NamedSQLQueryDefinition query) throws MappingException { 235 checkQueryExist(name); 236 sqlqueries.put( name.intern(), query ); 237 } 238 239 private void checkQueryExist(String name) throws MappingException { 240 if ( sqlqueries.containsKey(name) || queries.containsKey(name) ) { 241 throw new MappingException("Duplicate query named: " + name); 242 } 243 } 244 245 public void addResultSetMapping(ResultSetMappingDefinition sqlResultSetMapping) { 246 final String name = sqlResultSetMapping.getName(); 247 if ( resultSetMappings.containsKey(name) ) { 248 throw new MappingException("Duplicate ResultSet mapping named: " + name); 249 } 250 resultSetMappings.put(name, sqlResultSetMapping); 251 } 252 253 public ResultSetMappingDefinition getResultSetMapping(String name) { 254 return (ResultSetMappingDefinition) resultSetMappings.get(name); 255 } 256 257 258 public NamedQueryDefinition getQuery(String name) { 259 return (NamedQueryDefinition) queries.get(name); 260 } 261 262 public void addSecondPass(HbmBinder.SecondPass sp) { 263 addSecondPass(sp, false); 264 } 265 266 public void addSecondPass(HbmBinder.SecondPass sp, boolean onTopOfTheQueue) { 267 if (onTopOfTheQueue) { 268 secondPasses.add(0, sp); 269 } 270 else { 271 secondPasses.add(sp); 272 } 273 } 274 275 279 public boolean isAutoImport() { 280 return autoImport; 281 } 282 283 287 public void setAutoImport(boolean autoImport) { 288 this.autoImport = autoImport; 289 } 290 291 void addUniquePropertyReference(String referencedClass, String propertyName) { 292 PropertyReference upr = new PropertyReference(); 293 upr.referencedClass = referencedClass; 294 upr.propertyName = propertyName; 295 upr.unique = true; 296 propertyReferences.add(upr); 297 } 298 299 void addPropertyReference(String referencedClass, String propertyName) { 300 PropertyReference upr = new PropertyReference(); 301 upr.referencedClass = referencedClass; 302 upr.propertyName = propertyName; 303 propertyReferences.add(upr); 304 } 305 306 static final class PropertyReference implements Serializable { 307 String referencedClass; 308 String propertyName; 309 boolean unique; 310 } 311 312 315 public String getDefaultPackage() { 316 return defaultPackage; 317 } 318 319 322 public void setDefaultPackage(String defaultPackage) { 323 this.defaultPackage = defaultPackage; 324 } 325 326 public NamingStrategy getNamingStrategy() { 327 return namingStrategy; 328 } 329 330 public void addTypeDef(String typeName, String typeClass, Properties paramMap) { 331 TypeDef def = new TypeDef(typeClass, paramMap); 332 typeDefs.put(typeName, def); 333 log.debug("Added " + typeName + " with class " + typeClass); 334 } 335 336 public TypeDef getTypeDef(String typeName) { 337 return (TypeDef) typeDefs.get(typeName); 338 } 339 340 public Iterator iterateCollections() { 341 return collections.values().iterator(); 342 } 343 344 public Iterator iterateTables() { 345 return tables.values().iterator(); 346 } 347 348 public Map getFilterDefinitions() { 349 return filterDefinitions; 350 } 351 352 public void addFilterDefinition(FilterDefinition definition) { 353 filterDefinitions.put( definition.getFilterName(), definition ); 354 } 355 356 public FilterDefinition getFilterDefinition(String name) { 357 return (FilterDefinition) filterDefinitions.get(name); 358 } 359 360 public boolean isDefaultLazy() { 361 return defaultLazy; 362 } 363 public void setDefaultLazy(boolean defaultLazy) { 364 this.defaultLazy = defaultLazy; 365 } 366 367 371 public void addToExtendsQueue(String className, Document doc) { 372 List existingQueue = (List ) extendsQueue.get(className); 373 if(existingQueue==null) { 374 existingQueue = new ArrayList (); 375 } 376 existingQueue.add(doc); 377 extendsQueue.put(className, existingQueue); 378 } 379 380 public PersistentClass locatePersistentClassByEntityName(String entityName) { 381 PersistentClass persistentClass = ( PersistentClass ) classes.get( entityName ); 382 if ( persistentClass == null ) { 383 String actualEntityName = ( String ) imports.get( entityName ); 384 if ( StringHelper.isNotEmpty( actualEntityName ) ) { 385 persistentClass = ( PersistentClass ) classes.get( actualEntityName ); 386 } 387 } 388 return persistentClass; 389 } 390 } | Popular Tags |