📄 annotationconfiguration.java
字号:
// $Id: AnnotationConfiguration.java 14389 2008-03-04 21:22:12Z epbernard $package org.hibernate.cfg;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Collection;import java.util.Comparator;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.ResourceBundle;import java.util.Set;import java.util.SortedSet;import java.util.StringTokenizer;import java.util.TreeSet;import java.net.URL;import javax.persistence.Entity;import javax.persistence.MappedSuperclass;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import org.hibernate.AnnotationException;import org.hibernate.HibernateException;import org.hibernate.MappingException;import org.hibernate.SessionFactory;import org.hibernate.Interceptor;import org.hibernate.annotations.AnyMetaDef;import org.hibernate.annotations.common.reflection.ReflectionManager;import org.hibernate.annotations.common.reflection.XClass;import org.hibernate.cfg.annotations.Version;import org.hibernate.cfg.annotations.reflection.EJB3ReflectionManager;import org.hibernate.cfg.search.SearchConfiguration;import org.hibernate.event.PostDeleteEventListener;import org.hibernate.event.PostInsertEventListener;import org.hibernate.event.PostUpdateEventListener;import org.hibernate.event.PreInsertEventListener;import org.hibernate.event.PreUpdateEventListener;import org.hibernate.event.EventListeners;import org.hibernate.mapping.Column;import org.hibernate.mapping.Join;import org.hibernate.mapping.PersistentClass;import org.hibernate.mapping.Table;import org.hibernate.mapping.UniqueKey;import org.hibernate.util.JoinedIterator;import org.hibernate.util.ReflectHelper;import org.hibernate.util.StringHelper;import org.xml.sax.InputSource;import org.xml.sax.SAXException;/** * Similar to the {@link Configuration} object but handles EJB3 and Hibernate * specific annotations as a metadata facility * * @author Emmanuel Bernard */public class AnnotationConfiguration extends Configuration { private static Log log = LogFactory.getLog( AnnotationConfiguration.class ); static { Version.touch(); //touch version } public static final String ARTEFACT = "hibernate.mapping.precedence"; public static final String DEFAULT_PRECEDENCE = "hbm, class"; private Map namedGenerators; private Map<String, Map<String, Join>> joins; private Map<String, AnnotatedClassType> classTypes; private Set<String> defaultNamedQueryNames; private Set<String> defaultNamedNativeQueryNames; private Set<String> defaultSqlResulSetMappingNames; private Set<String> defaultNamedGenerators; private Map<String, Properties> generatorTables; private Map<Table, List<String[]>> tableUniqueConstraints; private Map<String, String> mappedByResolver; private Map<String, String> propertyRefResolver; private Map<String, AnyMetaDef> anyMetaDefs; private List<XClass> annotatedClasses; private Map<String, XClass> annotatedClassEntities; private Map<String, Document> hbmEntities; private List<CacheHolder> caches; private List<Document> hbmDocuments; //user ordering matters, hence the list private String precedence = null; private boolean inSecondPass = false; private transient ReflectionManager reflectionManager; private boolean isDefaultProcessed = false; private boolean isValidatorNotPresentLogged; public AnnotationConfiguration() { super(); } public AnnotationConfiguration(SettingsFactory sf) { super( sf ); } protected List<XClass> orderAndFillHierarchy(List<XClass> original) { //TODO remove embeddable List<XClass> copy = new ArrayList<XClass>( original ); //for each class, copy all the relevent hierarchy for (XClass clazz : original) { XClass superClass = clazz.getSuperclass(); while ( superClass != null && !reflectionManager.equals( superClass, Object.class ) && !copy.contains( superClass ) ) { if ( superClass.isAnnotationPresent( Entity.class ) || superClass.isAnnotationPresent( MappedSuperclass.class ) ) { copy.add( superClass ); } superClass = superClass.getSuperclass(); } } List<XClass> workingCopy = new ArrayList<XClass>( copy ); List<XClass> newList = new ArrayList<XClass>( copy.size() ); while ( workingCopy.size() > 0 ) { XClass clazz = workingCopy.get( 0 ); orderHierarchy( workingCopy, newList, copy, clazz ); } return newList; } private void orderHierarchy(List<XClass> copy, List<XClass> newList, List<XClass> original, XClass clazz) { if ( clazz == null || reflectionManager.equals( clazz, Object.class ) ) return; //process superclass first orderHierarchy( copy, newList, original, clazz.getSuperclass() ); if ( original.contains( clazz ) ) { if ( !newList.contains( clazz ) ) { newList.add( clazz ); } copy.remove( clazz ); } } /** * Read a mapping from the class annotation metadata (JSR 175). * * @param persistentClass the mapped class * @return the configuration object */ public AnnotationConfiguration addAnnotatedClass(Class persistentClass) throws MappingException { XClass persistentXClass = reflectionManager.toXClass( persistentClass ); try { annotatedClasses.add( persistentXClass ); return this; } catch (MappingException me) { log.error( "Could not compile the mapping annotations", me ); throw me; } } /** * Read package level metadata * * @param packageName java package name * @return the configuration object */ public AnnotationConfiguration addPackage(String packageName) throws MappingException { log.info( "Mapping package " + packageName ); try { AnnotationBinder.bindPackage( packageName, createExtendedMappings() ); return this; } catch (MappingException me) { log.error( "Could not compile the mapping annotations", me ); throw me; } } public ExtendedMappings createExtendedMappings() { return new ExtendedMappings( classes, collections, tables, namedQueries, namedSqlQueries, sqlResultSetMappings, defaultNamedQueryNames, defaultNamedNativeQueryNames, defaultSqlResulSetMappingNames, defaultNamedGenerators, imports, secondPasses, propertyReferences, namingStrategy, typeDefs, filterDefinitions, namedGenerators, joins, classTypes, extendsQueue, tableNameBinding, columnNameBindingPerTable, auxiliaryDatabaseObjects, generatorTables, tableUniqueConstraints, mappedByResolver, propertyRefResolver, anyMetaDefs, reflectionManager ); } @Override public void setCacheConcurrencyStrategy( String clazz, String concurrencyStrategy, String region, boolean cacheLazyProperty ) throws MappingException { caches.add( new CacheHolder( clazz, concurrencyStrategy, region, true, cacheLazyProperty ) ); } @Override public void setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy, String region) throws MappingException { caches.add( new CacheHolder( collectionRole, concurrencyStrategy, region, false, false ) ); } @Override protected void reset() { super.reset(); namedGenerators = new HashMap(); joins = new HashMap<String, Map<String, Join>>(); classTypes = new HashMap<String, AnnotatedClassType>(); generatorTables = new HashMap<String, Properties>(); defaultNamedQueryNames = new HashSet<String>(); defaultNamedNativeQueryNames = new HashSet<String>(); defaultSqlResulSetMappingNames = new HashSet<String>(); defaultNamedGenerators = new HashSet<String>(); tableUniqueConstraints = new HashMap<Table, List<String[]>>(); mappedByResolver = new HashMap<String, String>(); propertyRefResolver = new HashMap<String, String>(); annotatedClasses = new ArrayList<XClass>(); caches = new ArrayList<CacheHolder>(); hbmEntities = new HashMap<String, Document>(); annotatedClassEntities = new HashMap<String, XClass>(); hbmDocuments = new ArrayList<Document>(); namingStrategy = EJB3NamingStrategy.INSTANCE; setEntityResolver( new EJB3DTDEntityResolver() ); anyMetaDefs = new HashMap<String, AnyMetaDef>(); reflectionManager = new EJB3ReflectionManager(); } @Override protected void secondPassCompile() throws MappingException { log.debug( "Execute first pass mapping processing" ); //build annotatedClassEntities { List<XClass> tempAnnotatedClasses = new ArrayList<XClass>( annotatedClasses.size() ); for (XClass clazz : annotatedClasses) { if ( clazz.isAnnotationPresent( Entity.class ) ) { annotatedClassEntities.put( clazz.getName(), clazz ); tempAnnotatedClasses.add( clazz ); } else if ( clazz.isAnnotationPresent( MappedSuperclass.class ) ) { tempAnnotatedClasses.add( clazz ); } //only keep MappedSuperclasses and Entity in this list } annotatedClasses = tempAnnotatedClasses; } //process default values first if ( !isDefaultProcessed ) { AnnotationBinder.bindDefaults( createExtendedMappings() ); isDefaultProcessed = true; } //process entities if ( precedence == null ) precedence = getProperties().getProperty( ARTEFACT ); if ( precedence == null ) precedence = DEFAULT_PRECEDENCE; StringTokenizer precedences = new StringTokenizer( precedence, ",; ", false ); if ( !precedences.hasMoreElements() ) { throw new MappingException( ARTEFACT + " cannot be empty: " + precedence ); } while ( precedences.hasMoreElements() ) { String artifact = (String) precedences.nextElement(); removeConflictedArtifact( artifact ); processArtifactsOfType( artifact ); } int cacheNbr = caches.size(); for (int index = 0; index < cacheNbr; index++) { CacheHolder cacheHolder = caches.get( index ); if ( cacheHolder.isClass ) { super.setCacheConcurrencyStrategy( cacheHolder.role, cacheHolder.usage, cacheHolder.region, cacheHolder.cacheLazy ); } else { super.setCollectionCacheConcurrencyStrategy( cacheHolder.role, cacheHolder.usage, cacheHolder.region ); } } caches.clear(); try { inSecondPass = true; processFkSecondPassInOrder(); Iterator iter = secondPasses.iterator(); while ( iter.hasNext() ) { SecondPass sp = (SecondPass) iter.next(); //do the second pass of fk before the others and remove them if ( sp instanceof CreateKeySecondPass ) { sp.doSecondPass( classes ); iter.remove(); } } iter = secondPasses.iterator(); while ( iter.hasNext() ) { SecondPass sp = (SecondPass) iter.next(); //do the SecondaryTable second pass before any association becasue associations can be built on joins if ( sp instanceof SecondaryTableSecondPass ) { sp.doSecondPass( classes ); iter.remove(); } } super.secondPassCompile(); inSecondPass = false; } catch (RecoverableException e) { //the exception was not recoverable after all throw (RuntimeException) e.getCause(); } Iterator tables = tableUniqueConstraints.entrySet().iterator(); Table table; Map.Entry entry; String keyName; int uniqueIndexPerTable; while ( tables.hasNext() ) { entry = (Map.Entry) tables.next(); table = (Table) entry.getKey(); List<String[]> uniqueConstraints = (List<String[]>) entry.getValue(); uniqueIndexPerTable = 0; for (String[] columnNames : uniqueConstraints) { keyName = "key" + uniqueIndexPerTable++; buildUniqueKeyFromColumnNames( columnNames, table, keyName );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -