📄 configuration.java
字号:
if ( referencedClass == null ) { throw new MappingException( "An association from the table " + fk.getTable().getName() + " refers to an unmapped class: " + referencedEntityName ); } if ( referencedClass.isJoinedSubclass() ) { secondPassCompileForeignKeys( referencedClass.getSuperclass().getTable(), done ); } fk.setReferencedTable( referencedClass.getTable() ); fk.alignColumns(); } } } /** * Get the named queries */ public Map getNamedQueries() { return namedQueries; } /** * Instantiate a new <tt>SessionFactory</tt>, using the properties and * mappings in this configuration. The <tt>SessionFactory</tt> will be * immutable, so changes made to the <tt>Configuration</tt> after * building the <tt>SessionFactory</tt> will not affect it. * * @return a new factory for <tt>Session</tt>s * @see org.hibernate.SessionFactory */ public SessionFactory buildSessionFactory() throws HibernateException { log.debug( "Preparing to build session factory with filters : " + filterDefinitions ); secondPassCompile(); validate(); Environment.verifyProperties( properties ); Properties copy = new Properties(); copy.putAll( properties ); PropertiesHelper.resolvePlaceHolders( copy ); Settings settings = buildSettings( copy ); return new SessionFactoryImpl( this, mapping, settings, getInitializedEventListeners(), sessionFactoryObserver ); } private EventListeners getInitializedEventListeners() { EventListeners result = (EventListeners) eventListeners.shallowCopy(); result.initializeListeners( this ); return result; } /** * Return the configured <tt>Interceptor</tt> */ public Interceptor getInterceptor() { return interceptor; } /** * Get all properties */ public Properties getProperties() { return properties; } /** * Configure an <tt>Interceptor</tt> */ public Configuration setInterceptor(Interceptor interceptor) { this.interceptor = interceptor; return this; } /** * Specify a completely new set of properties */ public Configuration setProperties(Properties properties) { this.properties = properties; return this; } /** * Set the given properties */ public Configuration addProperties(Properties extraProperties) { this.properties.putAll( extraProperties ); return this; } /** * Adds the incoming properties to the internap properties structure, * as long as the internal structure does not already contain an * entry for the given key. * * @param properties * @return this */ public Configuration mergeProperties(Properties properties) { Iterator itr = properties.entrySet().iterator(); while ( itr.hasNext() ) { final Map.Entry entry = ( Map.Entry ) itr.next(); if ( this.properties.containsKey( entry.getKey() ) ) { continue; } this.properties.setProperty( ( String ) entry.getKey(), ( String ) entry.getValue() ); } return this; } /** * Set a property */ public Configuration setProperty(String propertyName, String value) { properties.setProperty( propertyName, value ); return this; } /** * Get a property */ public String getProperty(String propertyName) { return properties.getProperty( propertyName ); } private void addProperties(Element parent) { Iterator iter = parent.elementIterator( "property" ); while ( iter.hasNext() ) { Element node = (Element) iter.next(); String name = node.attributeValue( "name" ); String value = node.getText().trim(); log.debug( name + "=" + value ); properties.setProperty( name, value ); if ( !name.startsWith( "hibernate" ) ) { properties.setProperty( "hibernate." + name, value ); } } Environment.verifyProperties( properties ); } /** * Get the configuration file as an <tt>InputStream</tt>. Might be overridden * by subclasses to allow the configuration to be located by some arbitrary * mechanism. */ protected InputStream getConfigurationInputStream(String resource) throws HibernateException { log.info( "Configuration resource: " + resource ); return ConfigHelper.getResourceAsStream( resource ); } /** * Use the mappings and properties specified in an application * resource named <tt>hibernate.cfg.xml</tt>. */ public Configuration configure() throws HibernateException { configure( "/hibernate.cfg.xml" ); return this; } /** * Use the mappings and properties specified in the given application * resource. The format of the resource is defined in * <tt>hibernate-configuration-3.0.dtd</tt>. * <p/> * The resource is found via <tt>getConfigurationInputStream(resource)</tt>. */ public Configuration configure(String resource) throws HibernateException { log.info( "configuring from resource: " + resource ); InputStream stream = getConfigurationInputStream( resource ); return doConfigure( stream, resource ); } /** * Use the mappings and properties specified in the given document. * The format of the document is defined in * <tt>hibernate-configuration-3.0.dtd</tt>. * * @param url URL from which you wish to load the configuration * @return A configuration configured via the file * @throws HibernateException */ public Configuration configure(URL url) throws HibernateException { log.info( "configuring from url: " + url.toString() ); try { return doConfigure( url.openStream(), url.toString() ); } catch (IOException ioe) { throw new HibernateException( "could not configure from URL: " + url, ioe ); } } /** * Use the mappings and properties specified in the given application * file. The format of the file is defined in * <tt>hibernate-configuration-3.0.dtd</tt>. * * @param configFile <tt>File</tt> from which you wish to load the configuration * @return A configuration configured via the file * @throws HibernateException */ public Configuration configure(File configFile) throws HibernateException { log.info( "configuring from file: " + configFile.getName() ); try { return doConfigure( new FileInputStream( configFile ), configFile.toString() ); } catch (FileNotFoundException fnfe) { throw new HibernateException( "could not find file: " + configFile, fnfe ); } } /** * Use the mappings and properties specified in the given application * resource. The format of the resource is defined in * <tt>hibernate-configuration-3.0.dtd</tt>. * * @param stream Inputstream to be read from * @param resourceName The name to use in warning/error messages * @return A configuration configured via the stream * @throws HibernateException */ protected Configuration doConfigure(InputStream stream, String resourceName) throws HibernateException { org.dom4j.Document doc; try { List errors = new ArrayList(); doc = xmlHelper.createSAXReader( resourceName, errors, entityResolver ) .read( new InputSource( stream ) ); if ( errors.size() != 0 ) { throw new MappingException( "invalid configuration", (Throwable) errors.get( 0 ) ); } } catch (DocumentException e) { throw new HibernateException( "Could not parse configuration: " + resourceName, e ); } finally { try { stream.close(); } catch (IOException ioe) { log.warn( "could not close input stream for: " + resourceName, ioe ); } } return doConfigure( doc ); } /** * Use the mappings and properties specified in the given XML document. * The format of the file is defined in * <tt>hibernate-configuration-3.0.dtd</tt>. * * @param document an XML document from which you wish to load the configuration * @return A configuration configured via the <tt>Document</tt> * @throws HibernateException if there is problem in accessing the file. */ public Configuration configure(Document document) throws HibernateException { log.info( "configuring from XML document" ); return doConfigure( xmlHelper.createDOMReader().read( document ) ); } protected Configuration doConfigure(org.dom4j.Document doc) throws HibernateException { Element sfNode = doc.getRootElement().element( "session-factory" ); String name = sfNode.attributeValue( "name" ); if ( name != null ) { properties.setProperty( Environment.SESSION_FACTORY_NAME, name ); } addProperties( sfNode ); parseSessionFactory( sfNode, name ); Element secNode = doc.getRootElement().element( "security" ); if ( secNode != null ) { parseSecurity( secNode ); } log.info( "Configured SessionFactory: " + name ); log.debug( "properties: " + properties ); return this; } private void parseSessionFactory(Element sfNode, String name) { Iterator elements = sfNode.elementIterator(); while ( elements.hasNext() ) { Element subelement = (Element) elements.next(); String subelementName = subelement.getName(); if ( "mapping".equals( subelementName ) ) { parseMappingElement( subelement, name ); } else if ( "class-cache".equals( subelementName ) ) { String className = subelement.attributeValue( "class" ); Attribute regionNode = subelement.attribute( "region" ); final String region = ( regionNode == null ) ? className : regionNode.getValue(); boolean includeLazy = !"non-lazy".equals( subelement.attributeValue( "include" ) ); setCacheConcurrencyStrategy( className, subelement.attributeValue( "usage" ), region, includeLazy ); } else if ( "collection-cache".equals( subelementName ) ) { String role = subelement.attributeValue( "collection" ); Attribute regionNode = subelement.attribute( "region" ); final String region = ( regionNode == null ) ? role : regionNode.getValue(); setCollectionCacheConcurrencyStrategy( role, subelement.attributeValue( "usage" ), region ); } else if ( "listener".equals( subelementName ) ) { parseListener( subelement ); } else if ( "event".equals( subelementName ) ) { parseEvent( subelement ); } } } protected void parseMappingElement(Element subelement, String name) { Attribute rsrc = subelement.attribute( "resource" ); Attribute file = subelement.attribute( "file" ); Attribute jar = subelement.attribute( "jar" ); Attribute pkg = subelement.attribute( "package" ); Attribute clazz = subelement.attribute( "class" ); if ( rsrc != null ) { log.debug( name + "<-" + rsrc ); addResource( rsrc.getValue() ); } else if ( jar != null ) { log.debug( name + "<-" + jar ); addJar( new File( jar.getValue() ) ); } else if ( pkg != null ) { throw new MappingException( "An AnnotationConfiguration instance is required to use <mapping package=\"" + pkg.getValue() + "\"/>" ); } else if ( clazz != null ) { throw new MappingException( "An AnnotationConfiguration instance is required to use <mapping class=\"" + clazz.getValue() + "\"/>" ); } else { if ( file == null ) { throw new MappingException( "<mapping> element in configuration specifies no attributes" ); } log.debug( name + "<-" + file ); addFile( file.getValue() ); } } private void parseSecurity(Element secNode) { String contextId = secNode.attributeValue( "context" ); setProperty(Environment.JACC_CONTEXTID, contextId); log.info( "JACC contextID: " + contextId ); JACCConfiguration jcfg = new JACCConfiguration( contextId ); Iterator grantElements = secNode.elementIterator(); while ( grantElements.hasNext() ) { Element grantElement = (Element) grantElements.next(); String elementName = grantElement.getName(); if ( "grant".equals( elementName ) ) { jcfg.addPermission( grantElement.attributeValue( "role" ), grantElement.attributeValue( "entity-name" ), grantElement.attributeValue( "actions" ) ); } } } private void parseEvent(Element element) { String type = element.attributeValue( "type" ); List listeners = element.elements(); String[] listenerClasses = new String[ listeners.size() ]; for ( int i = 0; i < listeners.size() ; i++ ) { listenerClasses[i] = ( (Element) listeners.get( i ) ).attributeValue( "class" ); } log.debug( "Event listeners: " + type + "=" + StringHelper.toString( listenerClasses ) ); setListeners( type, listenerClasses ); } private void parseListener(Element element) { String type = element.attributeValue( "type" ); if ( type == null ) { throw new MappingException( "No type specified for listener" ); } String impl = element.attributeValue( "class" ); log.debug( "Event listener: " + type + "=" + impl ); setListeners( type, new String[]{impl} ); } public void setListener(String type, String listener) { String[] listeners = null; if ( listener != null ) { listeners = (String[]) Array.newInstance( String.class, 1 ); listeners[0] = listener; } setListeners( type, listeners ); } public void setListeners(String type, String[] listenerClasses) { Object[] listeners = null; if ( listenerClasses != null ) { listeners = (Object[]) Array.newInstance( eventListeners.getListenerClassFor(type), listenerClasses.length ); for ( int i = 0; i < listeners.length ; i++ ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -