📄 configuration.java
字号:
//$Id: Configuration.java 9155 2006-01-27 16:46:34Z steveebersole $
package org.hibernate.cfg;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.hibernate.EmptyInterceptor;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.FilterDefinition;
import org.hibernate.engine.Mapping;
import org.hibernate.event.AutoFlushEventListener;
import org.hibernate.event.DeleteEventListener;
import org.hibernate.event.DirtyCheckEventListener;
import org.hibernate.event.EventListeners;
import org.hibernate.event.EvictEventListener;
import org.hibernate.event.FlushEntityEventListener;
import org.hibernate.event.FlushEventListener;
import org.hibernate.event.InitializeCollectionEventListener;
import org.hibernate.event.LoadEventListener;
import org.hibernate.event.LockEventListener;
import org.hibernate.event.MergeEventListener;
import org.hibernate.event.PersistEventListener;
import org.hibernate.event.PostDeleteEventListener;
import org.hibernate.event.PostInsertEventListener;
import org.hibernate.event.PostLoadEventListener;
import org.hibernate.event.PostUpdateEventListener;
import org.hibernate.event.PreDeleteEventListener;
import org.hibernate.event.PreInsertEventListener;
import org.hibernate.event.PreLoadEventListener;
import org.hibernate.event.PreUpdateEventListener;
import org.hibernate.event.RefreshEventListener;
import org.hibernate.event.ReplicateEventListener;
import org.hibernate.event.SaveOrUpdateEventListener;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.impl.SessionFactoryImpl;
import org.hibernate.mapping.AuxiliaryDatabaseObject;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.IdentifierCollection;
import org.hibernate.mapping.Index;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.UniqueKey;
import org.hibernate.secure.JACCConfiguration;
import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
import org.hibernate.tool.hbm2ddl.TableMetadata;
import org.hibernate.type.SerializationException;
import org.hibernate.type.Type;
import org.hibernate.util.ArrayHelper;
import org.hibernate.util.CollectionHelper;
import org.hibernate.util.ConfigHelper;
import org.hibernate.util.ReflectHelper;
import org.hibernate.util.SerializationHelper;
import org.hibernate.util.StringHelper;
import org.hibernate.util.XMLHelper;
import org.hibernate.util.PropertiesHelper;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
/**
* An instance of <tt>Configuration</tt> allows the application
* to specify properties and mapping documents to be used when
* creating a <tt>SessionFactory</tt>. Usually an application will create
* a single <tt>Configuration</tt>, build a single instance of
* <tt>SessionFactory</tt> and then instantiate <tt>Session</tt>s in
* threads servicing client requests. The <tt>Configuration</tt> is meant
* only as an initialization-time object. <tt>SessionFactory</tt>s are
* immutable and do not retain any association back to the
* <tt>Configuration</tt>.<br>
* <br>
* A new <tt>Configuration</tt> will use the properties specified in
* <tt>hibernate.properties</tt> by default.
*
* @author Gavin King
* @see org.hibernate.SessionFactory
*/
public class Configuration implements Serializable {
private static Log log = LogFactory.getLog( Configuration.class );
protected Map classes;
protected Map imports;
protected Map collections;
protected Map tables;
protected List auxiliaryDatabaseObjects;
protected Map namedQueries;
protected Map namedSqlQueries;
/**
* Map<String, SqlResultSetMapping> result set name, result set description
*/
protected Map sqlResultSetMappings;
protected Map filterDefinitions;
protected List secondPasses;
protected List propertyReferences;
// protected List extendsQueue;
protected Map extendsQueue;
protected Map tableNameBinding;
protected Map columnNameBindingPerTable;
private Interceptor interceptor;
private Properties properties;
private EntityResolver entityResolver;
private transient XMLHelper xmlHelper;
protected transient Map typeDefs;
protected NamingStrategy namingStrategy;
private EventListeners eventListeners;
protected final SettingsFactory settingsFactory;
protected void reset() {
classes = new HashMap();
imports = new HashMap();
collections = new HashMap();
tables = new TreeMap();
namedQueries = new HashMap();
namedSqlQueries = new HashMap();
sqlResultSetMappings = new HashMap();
xmlHelper = new XMLHelper();
typeDefs = new HashMap();
propertyReferences = new ArrayList();
secondPasses = new ArrayList();
interceptor = EmptyInterceptor.INSTANCE;
properties = Environment.getProperties();
entityResolver = XMLHelper.DEFAULT_DTD_RESOLVER;
eventListeners = new EventListeners();
filterDefinitions = new HashMap();
// extendsQueue = new ArrayList();
extendsQueue = new HashMap();
auxiliaryDatabaseObjects = new ArrayList();
tableNameBinding = new HashMap();
columnNameBindingPerTable = new HashMap();
namingStrategy = DefaultNamingStrategy.INSTANCE;
}
private transient Mapping mapping = buildMapping();
protected Configuration(SettingsFactory settingsFactory) {
this.settingsFactory = settingsFactory;
reset();
}
public Configuration() {
this( new SettingsFactory() );
}
/**
* Iterate the class mappings
*/
public Iterator getClassMappings() {
return classes.values().iterator();
}
/**
* Iterate the collection mappings
*/
public Iterator getCollectionMappings() {
return collections.values().iterator();
}
/**
* Iterate the table mappings
*/
public Iterator getTableMappings() {
return tables.values().iterator();
}
/**
* Get the mapping for a particular class
*/
public PersistentClass getClassMapping(String persistentClass) {
return (PersistentClass) classes.get( persistentClass );
}
/**
* Get the mapping for a particular collection role
*
* @param role a collection role
* @return Collection
*/
public Collection getCollectionMapping(String role) {
return (Collection) collections.get( role );
}
/**
* Set a custom entity resolver. This entity resolver must be
* set before addXXX(misc) call.
* Default value is {@link org.hibernate.util.DTDEntityResolver}
*
* @param entityResolver entity resolver to use
*/
public void setEntityResolver(EntityResolver entityResolver) {
this.entityResolver = entityResolver;
}
/**
* Read mappings from a particular XML file
*
* @param xmlFile a path to a file
*/
public Configuration addFile(String xmlFile) throws MappingException {
log.info( "Reading mappings from file: " + xmlFile );
try {
List errors = new ArrayList();
org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile, errors, entityResolver )
.read( new File( xmlFile ) );
if ( errors.size() != 0 ) {
throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) );
}
add( doc );
return this;
}
catch (Exception e) {
throw new MappingException(
"Could not read mapping document from file: " + xmlFile,
e
);
}
}
/**
* Read mappings from a particular XML file
*
* @param xmlFile a path to a file
*/
public Configuration addFile(File xmlFile) throws MappingException {
log.info( "Reading mappings from file: " + xmlFile.getPath() );
try {
addInputStream( new FileInputStream( xmlFile ) );
}
catch (Exception e) {
throw new MappingException(
"Could not read mapping document from file: " + xmlFile.getPath(),
e
);
}
return this;
}
/**
* If a cached <tt>xmlFile + ".bin"</tt> exists and is newer than <tt>xmlFile</tt> the
* <tt>".bin"</tt> file will be read directly. Otherwise xmlFile is read and then
* serialized to <tt>xmlFile + ".bin"</tt> for use the next time.
*/
public Configuration addCacheableFile(File xmlFile) throws MappingException {
try {
File lazyfile = new File( xmlFile.getAbsolutePath() + ".bin" );
org.dom4j.Document doc = null;
List errors = new ArrayList();
final boolean useCachedFile = xmlFile.exists() &&
lazyfile.exists() &&
xmlFile.lastModified() < lazyfile.lastModified();
if ( useCachedFile ) {
try {
log.info( "Reading mappings from cache file: " + lazyfile );
doc = (org.dom4j.Document) SerializationHelper.deserialize( new FileInputStream( lazyfile ) );
}
catch (SerializationException e) {
log.warn( "Could not deserialize cache file: " + lazyfile.getPath(), e );
}
}
// If deserialization failed
if ( doc == null ) {
log.info( "Reading mappings from file: " + xmlFile );
doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver )
.read( xmlFile );
try {
log.debug( "Writing cache file for: " + xmlFile + " to: " + lazyfile );
SerializationHelper.serialize( (Serializable) doc, new FileOutputStream( lazyfile ) );
}
catch (SerializationException e) {
log.warn( "Could not write cached file: " + lazyfile, e );
}
}
if ( errors.size() != 0 ) {
throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) );
}
add( doc );
return this;
}
catch (Exception e) {
throw new MappingException(
"Could not read mapping document from file: " + xmlFile,
e
);
}
}
public Configuration addCacheableFile(String xmlFile) throws MappingException {
return addCacheableFile( new File( xmlFile ) );
}
/**
* Read mappings from a <tt>String</tt>
*
* @param xml an XML string
*/
public Configuration addXML(String xml) throws MappingException {
if ( log.isDebugEnabled() ) log.debug( "Mapping XML:\n" + xml );
try {
List errors = new ArrayList();
org.dom4j.Document doc = xmlHelper.createSAXReader( "XML String", errors, entityResolver )
.read( new StringReader( xml ) );
if ( errors.size() != 0 ) {
throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) );
}
add( doc );
}
catch (DocumentException e) {
throw new MappingException( "Could not parse mapping document in XML string", e );
}
return this;
}
/**
* Read mappings from a <tt>URL</tt>
*
* @param url
*/
public Configuration addURL(URL url) throws MappingException {
if ( log.isDebugEnabled() ) log.debug( "Reading mapping document from URL:" + url );
try {
addInputStream( url.openStream() );
}
catch (Exception e) {
throw new MappingException( "Could not read mapping document from URL: " + url, e );
}
return this;
}
/**
* Read mappings from a DOM <tt>Document</tt>
*
* @param doc a DOM document
*/
public Configuration addDocument(Document doc) throws MappingException {
if ( log.isDebugEnabled() ) log.debug( "Mapping document:\n" + doc );
add( xmlHelper.createDOMReader().read( doc ) );
return this;
}
protected void add(org.dom4j.Document doc) throws MappingException {
HbmBinder.bindRoot( doc, createMappings(), CollectionHelper.EMPTY_MAP );
}
/**
* Create a new <tt>Mappings</tt> to add class and collection
* mappings to.
*/
public Mappings createMappings() {
return new Mappings(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -