⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configuration.java

📁 一个Java持久层类库
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems reading the cached file or processing	 * the non-cached file.	 * @see #addCacheableFile(java.io.File)	 */	public Configuration addCacheableFile(String xmlFile) throws MappingException {		return addCacheableFile( new File( xmlFile ) );	}	/**	 * Read mappings from a <tt>String</tt>	 *	 * @param xml an XML string	 * @return this (for method chaining purposes)	 * @throws org.hibernate.MappingException Indicates problems parsing the	 * given 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 The url for the mapping document to be read.	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems reading the URL or processing	 * the mapping document.	 */	public Configuration addURL(URL url) throws MappingException {		if ( log.isDebugEnabled() ) {			log.debug( "Reading mapping document from URL:" + url.toExternalForm() );		}		try {			addInputStream( url.openStream() );		}		catch ( InvalidMappingException e ) {			throw new InvalidMappingException( "URL", url.toExternalForm(), e.getCause() );		}		catch (Exception e) {			throw new InvalidMappingException( "URL", url.toExternalForm(), e );		}		return this;	}	/**	 * Read mappings from a DOM <tt>Document</tt>	 *	 * @param doc The DOM document	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems reading the DOM or processing	 * the mapping document.	 */	public Configuration addDocument(Document doc) throws MappingException {		if ( log.isDebugEnabled() ) {			log.debug( "Mapping document:\n" + doc );		}		add( xmlHelper.createDOMReader().read( doc ) );		return this;	}	/**	 * Read mappings from an {@link java.io.InputStream}.	 *	 * @param xmlInputStream The input stream containing a DOM.	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems reading the stream, or	 * processing the contained mapping document.	 */	public Configuration addInputStream(InputStream xmlInputStream) throws MappingException {		try {			List errors = new ArrayList();			org.dom4j.Document doc = xmlHelper.createSAXReader( "XML InputStream", errors, entityResolver )					.read( new InputSource( xmlInputStream ) );			if ( errors.size() != 0 ) {				throw new InvalidMappingException( "invalid mapping", null, (Throwable) errors.get( 0 ) );			}			add( doc );			return this;		}		catch (DocumentException e) {			throw new InvalidMappingException( "input stream", null, e );		}		finally {			try {				xmlInputStream.close();			}			catch (IOException ioe) {				log.warn( "Could not close input stream", ioe );			}		}	}	/**	 * Read mappings as a application resource (i.e. classpath lookup).	 *	 * @param resourceName The resource name	 * @param classLoader The class loader to use.	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems locating the resource or	 * processing the contained mapping document.	 */	public Configuration addResource(String resourceName, ClassLoader classLoader) throws MappingException {		log.info( "Reading mappings from resource: " + resourceName );		InputStream rsrc = classLoader.getResourceAsStream( resourceName );		if ( rsrc == null ) {			throw new MappingNotFoundException( "resource", resourceName );		}		try {			return addInputStream( rsrc );		}		catch (MappingException me) {			throw new InvalidMappingException( "resource", resourceName, me );		}	}	/**	 * Read mappings as a application resourceName (i.e. classpath lookup)	 * trying different classloaders.	 *	 * @param resourceName The resource name	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems locating the resource or	 * processing the contained mapping document.	 */	public Configuration addResource(String resourceName) throws MappingException {		log.info( "Reading mappings from resource : " + resourceName );		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();		InputStream rsrc = null;		if (contextClassLoader!=null) {			rsrc = contextClassLoader.getResourceAsStream( resourceName );		}		if ( rsrc == null ) {			rsrc = Environment.class.getClassLoader().getResourceAsStream( resourceName );		}		if ( rsrc == null ) {			throw new MappingNotFoundException( "resource", resourceName );		}		try {			return addInputStream( rsrc );		}		catch (MappingException me) {			throw new InvalidMappingException( "resource", resourceName, me );		}	}	/**	 * Read a mapping as an application resouurce using the convention that a class	 * named <tt>foo.bar.Foo</tt> is mapped by a file <tt>foo/bar/Foo.hbm.xml</tt>	 * which can be resolved as a classpath resource.	 *	 * @param persistentClass The mapped class	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems locating the resource or	 * processing the contained mapping document.	 */	public Configuration addClass(Class persistentClass) throws MappingException {		String mappingResourceName = persistentClass.getName().replace( '.', '/' ) + ".hbm.xml";		log.info( "Reading mappings from resource: " + mappingResourceName );		return addResource( mappingResourceName, persistentClass.getClassLoader() );	}	/**	 * Read all mappings from a jar file	 * <p/>	 * Assumes that any file named <tt>*.hbm.xml</tt> is a mapping document.	 *	 * @param jar a jar file	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems reading the jar file or	 * processing the contained mapping documents.	 */	public Configuration addJar(File jar) throws MappingException {		log.info( "Searching for mapping documents in jar: " + jar.getName() );		JarFile jarFile = null;		try {			try {				jarFile = new JarFile( jar );			}			catch (IOException ioe) {				throw new InvalidMappingException(						"Could not read mapping documents from jar: " + jar.getName(), "jar", jar.getName(),						ioe				);			}			Enumeration jarEntries = jarFile.entries();			while ( jarEntries.hasMoreElements() ) {				ZipEntry ze = (ZipEntry) jarEntries.nextElement();				if ( ze.getName().endsWith( ".hbm.xml" ) ) {					log.info( "Found mapping document in jar: " + ze.getName() );					try {						addInputStream( jarFile.getInputStream( ze ) );					}					catch (Exception e) {						throw new InvalidMappingException(								"Could not read mapping documents from jar: " + jar.getName(),								"jar",								jar.getName(),								e						);					}				}			}		}		finally {			try {				if ( jarFile != null ) {					jarFile.close();				}			}			catch (IOException ioe) {				log.error("could not close jar", ioe);			}		}		return this;	}	/**	 * Read all mapping documents from a directory tree.	 * <p/>	 * Assumes that any file named <tt>*.hbm.xml</tt> is a mapping document.	 *	 * @param dir The directory	 * @return this (for method chaining purposes)	 * @throws MappingException Indicates problems reading the jar file or	 * processing the contained mapping documents.	 */	public Configuration addDirectory(File dir) throws MappingException {		File[] files = dir.listFiles();		for ( int i = 0; i < files.length ; i++ ) {			if ( files[i].isDirectory() ) {				addDirectory( files[i] );			}			else if ( files[i].getName().endsWith( ".hbm.xml" ) ) {				addFile( files[i] );			}		}		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(				classes,				collections,				tables,				namedQueries,				namedSqlQueries,				sqlResultSetMappings,				imports,				secondPasses,				propertyReferences,				namingStrategy,				typeDefs,				filterDefinitions,				extendsQueue,				auxiliaryDatabaseObjects,				tableNameBinding,				columnNameBindingPerTable			);	}	private Iterator iterateGenerators(Dialect dialect) throws MappingException {		TreeMap generators = new TreeMap();		String defaultCatalog = properties.getProperty( Environment.DEFAULT_CATALOG );		String defaultSchema = properties.getProperty( Environment.DEFAULT_SCHEMA );		Iterator iter = classes.values().iterator();		while ( iter.hasNext() ) {			PersistentClass pc = (PersistentClass) iter.next();			if ( !pc.isInherited() ) {				IdentifierGenerator ig = pc.getIdentifier()						.createIdentifierGenerator(								dialect,								defaultCatalog,								defaultSchema,								(RootClass) pc							);				if ( ig instanceof PersistentIdentifierGenerator ) {					generators.put( ( (PersistentIdentifierGenerator) ig ).generatorKey(), ig );				}			}		}		iter = collections.values().iterator();		while ( iter.hasNext() ) {			Collection collection = (Collection) iter.next();			if ( collection.isIdentified() ) {				IdentifierGenerator ig = ( (IdentifierCollection) collection ).getIdentifier()						.createIdentifierGenerator(								dialect,								defaultCatalog,								defaultSchema,								null							);				if ( ig instanceof PersistentIdentifierGenerator ) {					generators.put( ( (PersistentIdentifierGenerator) ig ).generatorKey(), ig );				}			}		}		return generators.values().iterator();	}	/**	 * Generate DDL for dropping tables	 *	 * @see org.hibernate.tool.hbm2ddl.SchemaExport	 */	public String[] generateDropSchemaScript(Dialect dialect) throws HibernateException {		secondPassCompile();		String defaultCatalog = properties.getProperty( Environment.DEFAULT_CATALOG );		String defaultSchema = properties.getProperty( Environment.DEFAULT_SCHEMA );		ArrayList script = new ArrayList( 50 );		// drop them in reverse order in case db needs it done that way...		ListIterator itr = auxiliaryDatabaseObjects.listIterator( auxiliaryDatabaseObjects.size() );		while ( itr.hasPrevious() ) {			AuxiliaryDatabaseObject object = (AuxiliaryDatabaseObject) itr.previous();			if ( object.appliesToDialect( dialect ) ) {				script.add( object.sqlDropString( dialect, defaultCatalog, defaultSchema ) );			}		}		if ( dialect.dropConstraints() ) {			Iterator iter = getTableMappings();			while ( iter.hasNext() ) {				Table table = (Table) iter.next();				if ( table.isPhysicalTable() ) {					Iterator subIter = table.getForeignKeyIterator();					while ( subIter.hasNext() ) {						ForeignKey fk = (ForeignKey) subIter.next();						if ( fk.isPhysicalConstraint() ) {							script.add(									fk.sqlDropString(											dialect,											defaultCatalog,											defaultSchema										)								);						}					}				}			}		}		Iterator iter = getTableMappings();		while ( iter.hasNext() ) {			Table table = (Table) iter.next();			if ( table.isPhysicalTable() ) {				/*Iterator subIter = table.getIndexIterator();				while ( subIter.hasNext() ) {					Index index = (Index) subIter.next();					if ( !index.isForeignKey() || !dialect.hasImplicitIndexForForeignKey() ) {						script.add( index.sqlDropString(dialect) );					}				}*/				script.add(						table.sqlDropString(								dialect,								defaultCatalog,								defaultSchema							)					);			}		}		iter = iterateGenerators( dialect );		while ( iter.hasNext() ) {			String[] lines = ( (PersistentIdentifierGenerator) iter.next() ).sqlDropStrings( dialect );			for ( int i = 0; i < lines.length ; i++ ) {				script.add( lines[i] );			}		}		return ArrayHelper.toStringArray( script );	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -