?? configuration.java
字號:
while ( subIter.hasNext() ) { UniqueKey uk = (UniqueKey) subIter.next(); if ( tableInfo==null || tableInfo.getIndexMetadata( uk.getFilterName() ) == null ) { script.add( uk.sqlCreateString(dialect, mapping) ); } }*/ } iter = iterateGenerators( dialect ); while ( iter.hasNext() ) { PersistentIdentifierGenerator generator = ( PersistentIdentifierGenerator ) iter.next(); Object key = generator.generatorKey(); if ( !databaseMetadata.isSequence( key ) && !databaseMetadata.isTable( key ) ) { String[] lines = generator.sqlCreateStrings( dialect ); for ( int i = 0; i < lines.length; i++ ) script.add( lines[i] ); } } return ArrayHelper.toStringArray( script ); } private void validate() throws MappingException { Iterator iter = classes.values().iterator(); while ( iter.hasNext() ) ( ( PersistentClass ) iter.next() ).validate( mapping ); iter = collections.values().iterator(); while ( iter.hasNext() ) ( ( Collection ) iter.next() ).validate( mapping ); } /** * 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(). */ public void buildMappings() { secondPassCompile(); } /** * Find the first possible element in the queue of extends. */ protected org.dom4j.Document findPossibleExtends() { Iterator iter = extendsQueue.entrySet().iterator(); while ( iter.hasNext() ) { Map.Entry entry = ( Entry ) iter.next(); String superclass = ( String ) entry.getKey(); if ( getClassMapping( superclass ) != null ) { List queue = (List) entry.getValue(); if(queue.isEmpty()) { iter.remove(); continue; } else { return ( org.dom4j.Document ) queue.remove(0); } } } return null; } // This method may be called many times!! protected void secondPassCompile() throws MappingException { log.info( "processing extends queue" ); processExtendsQueue(); log.info( "processing collection mappings" ); Iterator iter = secondPasses.iterator(); while ( iter.hasNext() ) { HbmBinder.SecondPass sp = ( HbmBinder.SecondPass ) iter.next(); sp.doSecondPass( classes, CollectionHelper.EMPTY_MAP ); // TODO: align meta-attributes with normal bind... iter.remove(); } log.info( "processing association property references" ); iter = propertyReferences.iterator(); while ( iter.hasNext() ) { Mappings.PropertyReference upr = ( Mappings.PropertyReference ) iter.next(); PersistentClass clazz = getClassMapping( upr.referencedClass ); if ( clazz == null ) throw new MappingException( "property-ref to unmapped class: " + upr.referencedClass ); boolean found = false; Iterator propIter = clazz.isJoinedSubclass() ? clazz.getPropertyIterator() : clazz.getPropertyClosureIterator(); while ( propIter.hasNext() ) { Property prop = ( Property ) propIter.next(); if ( upr.propertyName.equals( prop.getName() ) ) { if ( upr.unique ) { ( ( SimpleValue ) prop.getValue() ).setAlternateUniqueKey( true ); } found = true; break; } } if ( !found ) { throw new MappingException( "property-ref not found: " + upr.propertyName + " in class: " + upr.referencedClass ); } } //TODO: Somehow add the newly created foreign keys to the internal collection log.info( "processing foreign key constraints" ); iter = getTableMappings(); Set done = new HashSet(); while ( iter.hasNext() ) secondPassCompileForeignKeys( ( Table ) iter.next(), done ); } /** * Try to empty the extends queue. */ private void processExtendsQueue() { org.dom4j.Document document = findPossibleExtends(); while ( document != null ) { add( document ); document = findPossibleExtends(); } if ( extendsQueue.size() > 0 ) { Iterator iterator = extendsQueue.keySet().iterator(); StringBuffer buf = new StringBuffer( "Following superclasses referenced in extends not found: " ); while ( iterator.hasNext() ) { String element = ( String ) iterator.next(); buf.append( element ); if ( iterator.hasNext() ) buf.append( "," ); } throw new MappingException( buf.toString() ); } } protected void secondPassCompileForeignKeys(Table table, Set done) throws MappingException { table.createForeignKeys(); Iterator iter = table.getForeignKeyIterator(); while ( iter.hasNext() ) { ForeignKey fk = ( ForeignKey ) iter.next(); if ( !done.contains( fk ) ) { done.add( fk ); final String referencedEntityName = fk.getReferencedEntityName(); if (referencedEntityName==null) { throw new MappingException("An association from the table "+ fk.getTable().getName() + " does not specify the referenced entity" ); } if ( log.isDebugEnabled() ) { log.debug( "resolving reference to class: " + referencedEntityName ); } 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() ); } } } /** * Get the named queries */ public Map getNamedQueries() { return namedQueries; } private static final Interceptor EMPTY_INTERCEPTOR = new EmptyInterceptor(); static final class EmptyInterceptor implements Interceptor, Serializable { public void onDelete( Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {} public boolean onFlushDirty( Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { return false; } public boolean onLoad( Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { return false; } public boolean onSave( Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { return false; } public void postFlush(Iterator entities) {} public void preFlush(Iterator entities) {} public Boolean isTransient(Object entity) { return null; } public Object instantiate(String entityName, EntityMode entityMode, Serializable id) { return null; } public int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { return null; } public String getEntityName(Object object) { return null; } public Object getEntity(String entityName, Serializable id) { return null; } public void afterTransactionBegin(Transaction tx) {} public void afterTransactionCompletion(Transaction tx) {} public void beforeTransactionCompletion(Transaction tx) {} } /** * 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 ); Settings settings = buildSettings(); return new SessionFactoryImpl( this, mapping, settings, sessionEventListenerConfig.shallowCopy() ); } /** * 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; } /** * 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 ); InputStream stream = Environment.class.getResourceAsStream( resource ); if ( stream == null ) stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( resource ); if ( stream == null ) { log.warn( resource + " not found" ); throw new HibernateException( resource + " not found" ); } return stream; } /** * 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-2.2.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>. *
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -