Package org.hibernate.cfg
Class Configuration
java.lang.Object
org.hibernate.cfg.Configuration
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AnnotationConfiguration
An instance of Configuration allows the application
to specify properties and mapping documents to be used when
creating a SessionFactory. Usually an application will create
a single Configuration, build a single instance of
SessionFactory and then instantiate Sessions in
threads servicing client requests. The Configuration is meant
only as an initialization-time object. SessionFactorys are
immutable and do not retain any association back to the
Configuration.
A new Configuration will use the properties specified in hibernate.properties by default.
A new Configuration will use the properties specified in hibernate.properties by default.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classprotected class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringprotected List<AuxiliaryDatabaseObject> protected Map<String, PersistentClass> protected Map<String, Collection> protected Mapstatic final MetadataSourceType[]static final StringSetting used to give the name of the defaultCacheConcurrencyStrategyto use when either@Cacheableor@Cacheis used.protected Map<ExtendsQueueEntry, ?> protected Map<String, FetchProfile> protected Map<String, FilterDefinition> protected Configuration.MetadataSourceQueueprotected Map<String, NamedQueryDefinition> protected Map<String, NamedSQLQueryDefinition> protected NamingStrategyprotected List<Mappings.PropertyReference> protected List<SecondPass> protected final SettingsFactoryprotected Map<String, SQLFunction> protected Map<String, ResultSetMappingDefinition> protected Mapstatic final StringSetting which indicates whether or not the newIdentifierGeneratorare used for AUTO, TABLE and SEQUENCE.protected XMLHelper -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(XmlDocument metadataXml) addAnnotatedClass(Class annotatedClass) Read metadata from the annotations associated with this class.voidaddCacheableFile(File xmlFile) Add a cached mapping file.addCacheableFile(String xmlFile) Add a cacheable mapping file.addCacheableFileStrictly(File xmlFile) INTENDED FOR TESTSUITE USE ONLY!Read a mapping as an application resource using the convention that a class named foo.bar.Foo is mapped by a file foo/bar/Foo.hbm.xml which can be resolved as a classpath resource.addDirectory(File dir) Read all mapping documents from a directory tree.addDocument(Document doc) Read mappings from a DOM DocumentvoidaddFetchProfile(FetchProfile fetchProfile) Read mappings from a particular XML fileRead mappings from a particular XML filevoidaddFilterDefinition(FilterDefinition definition) addInputStream(InputStream xmlInputStream) Read mappings from anInputStream.Read all mappings from a jar fileaddPackage(String packageName) Read package-level metadata.addProperties(Properties extraProperties) Add the given properties to ours.addResource(String resourceName) Read mappings as a application resourceName (i.e.addResource(String resourceName, ClassLoader classLoader) Read mappings as a application resource (i.e.voidaddSqlFunction(String functionName, SQLFunction function) Read mappings from a URLRead mappings from a StringvoidCall this to ensure the mappings are fully compiled/built.Create aSessionFactoryusing the properties and mappings in this configuration.Create an object-oriented view of the configuration propertiesbuildSettings(Properties props) Use the mappings and properties specified in an application resource named hibernate.cfg.xml.Use the mappings and properties specified in the given application file.Use the mappings and properties specified in the given application resource.Use the mappings and properties specified in the given document.Use the mappings and properties specified in the given XML document.Create a new Mappings to add class and collection mappings to.protected ConfigurationdoConfigure(InputStream stream, String resourceName) Configure this configuration's state from the contents of the given input stream.protected ConfigurationParse a dom4j document conforming to the Hibernate Configuration DTD (hibernate-configuration-3.0.dtd) and use its information to configure thisConfiguration's stateprotected ExtendsQueueEntryString[]generateDropSchemaScript(Dialect dialect) Generate DDL for dropping tablesString[]generateSchemaCreationScript(Dialect dialect) String[]generateSchemaUpdateScript(Dialect dialect, DatabaseMetadata databaseMetadata) getClassMapping(String entityName) Get the mapping for a particular entityIterate the entity mappingsgetCollectionMapping(String role) Get the mapping for a particular collection roleIterate the collection mappingsprotected InputStreamgetConfigurationInputStream(String resource) Get the configuration file as an InputStream.Retrieve the user-supplied delegate to handle non-existent entity scenarios.Retrieve the IdentifierGeneratorFactory in effect for this configuration.Get the query language importsRterieve the configuredInterceptor.Iterate the mapped super class mappings EXPERIMENTAL Consider this API as PRIVATEGet all propertiesgetProperty(String propertyName) Get a property value by nameIterate the table mappingsmergeProperties(Properties properties) Adds the incoming properties to the internal properties structure, as long as the internal structure does not already contain an entry for the given key.voidAllows registration of a type into the type registry.voidregisterTypeOverride(CompositeUserType type, String[] keys) voidregisterTypeOverride(UserType type, String[] keys) protected voidreset()protected voidprotected voidsecondPassCompileForeignKeys(Table table, Set done) setCacheConcurrencyStrategy(String entityName, String concurrencyStrategy) Set up a cache for an entity classsetCacheConcurrencyStrategy(String entityName, String concurrencyStrategy, String region) Set up a cache for an entity class, giving an explicit region namevoidsetCacheConcurrencyStrategy(String entityName, String concurrencyStrategy, String region, boolean cacheLazyProperty) setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) Set up a cache for a collection rolevoidsetCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy, String region) Set up a cache for a collection role, giving an explicit region namevoidsetEntityNotFoundDelegate(EntityNotFoundDelegate entityNotFoundDelegate) Specify a user-supplied delegate to be used to handle scenarios where an entity could not be located by specified id.voidsetEntityResolver(EntityResolver entityResolver) Set a custom entity resolver.setInterceptor(Interceptor interceptor) Set the currentInterceptorvoidsetListener(String type, Object listener) voidsetListener(String type, String listener) voidsetListeners(String type, Object[] listeners) voidsetListeners(String type, String[] listenerClasses) setNamingStrategy(NamingStrategy namingStrategy) Set a custom naming strategysetPersisterClassProvider(PersisterClassProvider persisterClassProvider) Defines a custom persister class provider.voidsetPrecedence(String precedence) setProperties(Properties properties) Specify a completely new set of propertiessetProperty(String propertyName, String value) Set a property value by namevoidsetSessionFactoryObserver(SessionFactoryObserver sessionFactoryObserver) voidvalidateSchema(Dialect dialect, DatabaseMetadata databaseMetadata)
-
Field Details
-
DEFAULT_CACHE_CONCURRENCY_STRATEGY
Setting used to give the name of the defaultCacheConcurrencyStrategyto use when either@Cacheableor@Cacheis used.@Cache(strategy="..")is used to override.- See Also:
-
USE_NEW_ID_GENERATOR_MAPPINGS
Setting which indicates whether or not the newIdentifierGeneratorare used for AUTO, TABLE and SEQUENCE. Default to false to keep backward compatibility.- See Also:
-
ARTEFACT_PROCESSING_ORDER
- See Also:
-
metadataSourceQueue
-
classes
-
imports
-
collections
-
tables
-
auxiliaryDatabaseObjects
-
namedQueries
-
namedSqlQueries
-
sqlResultSetMappings
-
typeDefs
-
filterDefinitions
-
fetchProfiles
-
tableNameBinding
-
columnNameBindingPerTable
-
secondPasses
-
propertyReferences
-
extendsQueue
-
sqlFunctions
-
xmlHelper
-
namingStrategy
-
settingsFactory
-
DEFAULT_ARTEFACT_PROCESSING_ORDER
-
-
Constructor Details
-
Configuration
-
Configuration
public Configuration()
-
-
Method Details
-
reset
protected void reset() -
getEntityTuplizerFactory
-
getReflectionManager
-
getClassMappings
Iterate the entity mappings- Returns:
- Iterator of the entity mappings currently contained in the configuration.
-
getCollectionMappings
Iterate the collection mappings- Returns:
- Iterator of the collection mappings currently contained in the configuration.
-
getTableMappings
Iterate the table mappings- Returns:
- Iterator of the table mappings currently contained in the configuration.
-
getMappedSuperclassMappings
Iterate the mapped super class mappings EXPERIMENTAL Consider this API as PRIVATE- Returns:
- iterator over the MappedSuperclass mapping currently contained in the configuration.
-
getClassMapping
Get the mapping for a particular entity- Parameters:
entityName- An entity name.- Returns:
- the entity mapping information
-
getCollectionMapping
Get the mapping for a particular collection role- Parameters:
role- a collection role- Returns:
- The collection mapping information
-
setEntityResolver
Set a custom entity resolver. This entity resolver must be set before addXXX(misc) call. Default value isDTDEntityResolver- Parameters:
entityResolver- entity resolver to use
-
getEntityResolver
-
getEntityNotFoundDelegate
Retrieve the user-supplied delegate to handle non-existent entity scenarios. May be null.- Returns:
- The user-supplied delegate
-
setEntityNotFoundDelegate
Specify a user-supplied delegate to be used to handle scenarios where an entity could not be located by specified id. This is mainly intended for EJB3 implementations to be able to control how proxy initialization errors should be handled...- Parameters:
entityNotFoundDelegate- The delegate to use
-
addFile
Read mappings from a particular XML file- Parameters:
xmlFile- a path to a file- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates inability to locate or parse the specified mapping file.- See Also:
-
addFile
Read mappings from a particular XML file- Parameters:
xmlFile- a path to a file- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates inability to locate the specified mapping file. Historically this could have indicated a problem parsing the XML document, but that is now delayed until afterbuildMappings()
-
add
-
addCacheableFile
Add a cached mapping file. A cached file is a serialized representation of the DOM structure of a particular mapping. It is saved from a previous call as a file with the name xmlFile + ".bin" where xmlFile is the name of the original mapping file. If a cached xmlFile + ".bin" exists and is newer than xmlFile the ".bin" file will be read directly. Otherwise xmlFile is read and then serialized to xmlFile + ".bin" for use the next time.- Parameters:
xmlFile- The cacheable mapping file to be added.- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems reading the cached file or processing the non-cached file.
-
addCacheableFileStrictly
public Configuration addCacheableFileStrictly(File xmlFile) throws SerializationException, FileNotFoundException INTENDED FOR TESTSUITE USE ONLY! Much likeaddCacheableFile(File)except that here we will fail immediately if the cache version cannot be found or used for whatever reason- Parameters:
xmlFile- The xml file, not the bin!- Returns:
- The dom "deserialized" from the cached file.
- Throws:
SerializationException- Indicates a problem deserializing the cached dom treeFileNotFoundException- Indicates that the cached file was not found or was not usable.
-
addCacheableFile
Add a cacheable mapping file.- Parameters:
xmlFile- The name of the file to be added. This must be in a form useable to simply construct aFileinstance.- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems reading the cached file or processing the non-cached file.- See Also:
-
addXML
Read mappings from a String- Parameters:
xml- an XML string- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems parsing the given XML string
-
addURL
Read mappings from a URL- Parameters:
url- The url for the mapping document to be read.- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems reading the URL or processing the mapping document.
-
addDocument
Read mappings from a DOM Document- Parameters:
doc- The DOM document- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems reading the DOM or processing the mapping document.
-
addInputStream
Read mappings from anInputStream.- Parameters:
xmlInputStream- The input stream containing a DOM.- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems reading the stream, or processing the contained mapping document.
-
addResource
public Configuration addResource(String resourceName, ClassLoader classLoader) throws MappingException Read mappings as a application resource (i.e. classpath lookup).- Parameters:
resourceName- The resource nameclassLoader- The class loader to use.- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems locating the resource or processing the contained mapping document.
-
addResource
Read mappings as a application resourceName (i.e. classpath lookup) trying different class loaders.- Parameters:
resourceName- The resource name- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems locating the resource or processing the contained mapping document.
-
addClass
Read a mapping as an application resource using the convention that a class named foo.bar.Foo is mapped by a file foo/bar/Foo.hbm.xml which can be resolved as a classpath resource.- Parameters:
persistentClass- The mapped class- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems locating the resource or processing the contained mapping document.
-
addAnnotatedClass
Read metadata from the annotations associated with this class.- Parameters:
annotatedClass- The class containing annotations- Returns:
- this (for method chaining)
-
addPackage
Read package-level metadata.- Parameters:
packageName- java package name- Returns:
- this (for method chaining)
- Throws:
MappingException- in case there is an error in the mapping data
-
addJar
Read all mappings from a jar file Assumes that any file named *.hbm.xml is a mapping document.- Parameters:
jar- a jar file- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems reading the jar file or processing the contained mapping documents.
-
addDirectory
Read all mapping documents from a directory tree. Assumes that any file named *.hbm.xml is a mapping document.- Parameters:
dir- The directory- Returns:
- this (for method chaining purposes)
- Throws:
MappingException- Indicates problems reading the jar file or processing the contained mapping documents.
-
createMappings
Create a new Mappings to add class and collection mappings to.- Returns:
- The created mappings
-
generateDropSchemaScript
Generate DDL for dropping tables- Parameters:
dialect- The dialect for which to generate the drop script- Returns:
- The sequence of DDL commands to drop the schema objects
- Throws:
HibernateException- Generally indicates a problem callingbuildMappings()- See Also:
-
generateSchemaCreationScript
- Parameters:
dialect- The dialect for which to generate the creation script- Returns:
- The sequence of DDL commands to create the schema objects
- Throws:
HibernateException- Generally indicates a problem callingbuildMappings()- See Also:
-
generateSchemaUpdateScript
public String[] generateSchemaUpdateScript(Dialect dialect, DatabaseMetadata databaseMetadata) throws HibernateException - Parameters:
dialect- The dialect for which to generate the creation scriptdatabaseMetadata- The database catalog information for the database to be updated; needed to work out what should be created/altered- Returns:
- The sequence of DDL commands to apply the schema objects
- Throws:
HibernateException- Generally indicates a problem callingbuildMappings()- See Also:
-
validateSchema
public void validateSchema(Dialect dialect, DatabaseMetadata databaseMetadata) throws HibernateException - Throws:
HibernateException
-
buildMappings
public void buildMappings()Call this to ensure the mappings are fully compiled/built. Usefull to ensure getting access to all information in the metamodel when calling e.g. getClassMappings(). -
secondPassCompile
- Throws:
MappingException
-
findPossibleExtends
-
secondPassCompileForeignKeys
- Throws:
MappingException
-
getNamedQueries
-
buildSessionFactory
Create aSessionFactoryusing the properties and mappings in this configuration. TheSessionFactorywill be immutable, so changes made tothisConfigurationafter building theSessionFactorywill not affect it.- Returns:
- The build
SessionFactory - Throws:
HibernateException- usually indicates an invalid configuration or invalid mapping information
-
getInterceptor
Rterieve the configuredInterceptor.- Returns:
- The current
Interceptor
-
setInterceptor
Set the currentInterceptor- Parameters:
interceptor- TheInterceptorto use for thebuiltSessionFactory.- Returns:
- this for method chaining
-
getProperties
Get all properties- Returns:
- all properties
-
getProperty
Get a property value by name- Parameters:
propertyName- The name of the property- Returns:
- The value curently associated with that property name; may be null.
-
setProperties
Specify a completely new set of properties- Parameters:
properties- The new set of properties- Returns:
- this for method chaining
-
addProperties
Add the given properties to ours.- Parameters:
extraProperties- The properties to add.- Returns:
- this for method chaining
-
mergeProperties
Adds the incoming properties to the internal properties structure, as long as the internal structure does not already contain an entry for the given key.- Parameters:
properties- The properties to merge- Returns:
- this for ethod chaining
-
setProperty
Set a property value by name- Parameters:
propertyName- The name of the property to setvalue- The new property value- Returns:
- this for method chaining
-
configure
Use the mappings and properties specified in an application resource named hibernate.cfg.xml.- Returns:
- this for method chaining
- Throws:
HibernateException- Generally indicates we cannot find hibernate.cfg.xml- See Also:
-
configure
Use the mappings and properties specified in the given application resource. The format of the resource is defined in hibernate-configuration-3.0.dtd. The resource is found viagetConfigurationInputStream(java.lang.String)- Parameters:
resource- The resource to use- Returns:
- this for method chaining
- Throws:
HibernateException- Generally indicates we cannot find the named resource- See Also:
-
getConfigurationInputStream
Get the configuration file as an InputStream. Might be overridden by subclasses to allow the configuration to be located by some arbitrary mechanism. By default here we use classpath resource resolution- Parameters:
resource- The resource to locate- Returns:
- The stream
- Throws:
HibernateException- Generally indicates we cannot find the named resource
-
configure
Use the mappings and properties specified in the given document. The format of the document is defined in hibernate-configuration-3.0.dtd.- Parameters:
url- URL from which you wish to load the configuration- Returns:
- this for method chaining
- Throws:
HibernateException- Generally indicates a problem access the url- See Also:
-
configure
Use the mappings and properties specified in the given application file. The format of the file is defined in hibernate-configuration-3.0.dtd.- Parameters:
configFile- File from which you wish to load the configuration- Returns:
- this for method chaining
- Throws:
HibernateException- Generally indicates a problem access the file- See Also:
-
doConfigure
protected Configuration doConfigure(InputStream stream, String resourceName) throws HibernateException Configure this configuration's state from the contents of the given input stream. The expectation is that the stream contents represent an XML document conforming to the Hibernate Configuration DTD. SeedoConfigure(Document)for further details.- Parameters:
stream- The input stream from which to readresourceName- The name to use in warning/error messages- Returns:
- this for method chaining
- Throws:
HibernateException- Indicates a problem reading the stream contents.
-
configure
Use the mappings and properties specified in the given XML document. The format of the file is defined in hibernate-configuration-3.0.dtd.- Parameters:
document- an XML document from which you wish to load the configuration- Returns:
- A configuration configured via the Document
- Throws:
HibernateException- if there is problem in accessing the file.
-
doConfigure
Parse a dom4j document conforming to the Hibernate Configuration DTD (hibernate-configuration-3.0.dtd) and use its information to configure thisConfiguration's state- Parameters:
doc- The dom4j document- Returns:
- this for method chaining
- Throws:
HibernateException- Indicates a problem performing the configuration task
-
setListener
-
setListeners
-
setListener
-
setListeners
-
getEventListeners
-
setCacheConcurrencyStrategy
Set up a cache for an entity class- Parameters:
entityName- The name of the entity to which we shoudl associate these cache settingsconcurrencyStrategy- The cache strategy to use- Returns:
- this for method chaining
-
setCacheConcurrencyStrategy
public Configuration setCacheConcurrencyStrategy(String entityName, String concurrencyStrategy, String region) Set up a cache for an entity class, giving an explicit region name- Parameters:
entityName- The name of the entity to which we should associate these cache settingsconcurrencyStrategy- The cache strategy to useregion- The name of the cache region to use- Returns:
- this for method chaining
-
setCacheConcurrencyStrategy
public void setCacheConcurrencyStrategy(String entityName, String concurrencyStrategy, String region, boolean cacheLazyProperty) throws MappingException - Throws:
MappingException
-
setCollectionCacheConcurrencyStrategy
public Configuration setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) Set up a cache for a collection role- Parameters:
collectionRole- The name of the collection to which we should associate these cache settingsconcurrencyStrategy- The cache strategy to use- Returns:
- this for method chaining
-
setCollectionCacheConcurrencyStrategy
public void setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy, String region) Set up a cache for a collection role, giving an explicit region name- Parameters:
collectionRole- The name of the collection to which we should associate these cache settingsconcurrencyStrategy- The cache strategy to useregion- The name of the cache region to use
-
getImports
Get the query language imports- Returns:
- a mapping from "import" names to fully qualified class names
-
buildSettings
Create an object-oriented view of the configuration properties- Returns:
- The build settings
-
buildSettings
- Throws:
HibernateException
-
getNamedSQLQueries
-
getSqlResultSetMappings
-
getNamingStrategy
-
setNamingStrategy
Set a custom naming strategy- Parameters:
namingStrategy- the NamingStrategy to set- Returns:
- this for method chaining
-
getPersisterClassProvider
-
setPersisterClassProvider
Defines a custom persister class provider. The persister class is chosen according to the following rules in decreasing priority: - the persister class defined explicitly via annotation or XML - the persister class returned by the PersisterClassProvider implementation (if not null) - the default provider as chosen by Hibernate Core (best choice most of the time)- Parameters:
persisterClassProvider- implementation
-
getIdentifierGeneratorFactory
Retrieve the IdentifierGeneratorFactory in effect for this configuration.- Returns:
- This configuration's IdentifierGeneratorFactory.
-
buildMapping
-
getFilterDefinitions
-
addFilterDefinition
-
iterateFetchProfiles
-
addFetchProfile
-
addAuxiliaryDatabaseObject
-
getSqlFunctions
-
addSqlFunction
-
getTypeResolver
-
registerTypeOverride
Allows registration of a type into the type registry. The phrase 'override' in the method name simply reminds that registration *potentially* replaces a previously registered type .- Parameters:
type- The type to register.
-
registerTypeOverride
-
registerTypeOverride
-
getSessionFactoryObserver
-
setSessionFactoryObserver
-
setPrecedence
-