📄 hbmbinder.java
字号:
// $Id: HbmBinder.java 11496 2007-05-09 03:54:06Z steve.ebersole@jboss.com $package org.hibernate.cfg;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Properties;import java.util.StringTokenizer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.Element;import org.hibernate.CacheMode;import org.hibernate.EntityMode;import org.hibernate.FetchMode;import org.hibernate.FlushMode;import org.hibernate.MappingException;import org.hibernate.engine.FilterDefinition;import org.hibernate.engine.NamedQueryDefinition;import org.hibernate.engine.Versioning;import org.hibernate.engine.ExecuteUpdateResultCheckStyle;import org.hibernate.id.PersistentIdentifierGenerator;import org.hibernate.mapping.Any;import org.hibernate.mapping.Array;import org.hibernate.mapping.AuxiliaryDatabaseObject;import org.hibernate.mapping.Backref;import org.hibernate.mapping.Bag;import org.hibernate.mapping.Collection;import org.hibernate.mapping.Column;import org.hibernate.mapping.Component;import org.hibernate.mapping.DependantValue;import org.hibernate.mapping.Fetchable;import org.hibernate.mapping.Filterable;import org.hibernate.mapping.Formula;import org.hibernate.mapping.IdentifierBag;import org.hibernate.mapping.IdentifierCollection;import org.hibernate.mapping.IndexBackref;import org.hibernate.mapping.IndexedCollection;import org.hibernate.mapping.Join;import org.hibernate.mapping.JoinedSubclass;import org.hibernate.mapping.KeyValue;import org.hibernate.mapping.List;import org.hibernate.mapping.ManyToOne;import org.hibernate.mapping.Map;import org.hibernate.mapping.MetaAttribute;import org.hibernate.mapping.OneToMany;import org.hibernate.mapping.OneToOne;import org.hibernate.mapping.PersistentClass;import org.hibernate.mapping.PrimitiveArray;import org.hibernate.mapping.Property;import org.hibernate.mapping.PropertyGeneration;import org.hibernate.mapping.RootClass;import org.hibernate.mapping.Selectable;import org.hibernate.mapping.Set;import org.hibernate.mapping.SimpleAuxiliaryDatabaseObject;import org.hibernate.mapping.SimpleValue;import org.hibernate.mapping.SingleTableSubclass;import org.hibernate.mapping.Subclass;import org.hibernate.mapping.Table;import org.hibernate.mapping.ToOne;import org.hibernate.mapping.TypeDef;import org.hibernate.mapping.UnionSubclass;import org.hibernate.mapping.UniqueKey;import org.hibernate.mapping.Value;import org.hibernate.persister.entity.JoinedSubclassEntityPersister;import org.hibernate.persister.entity.SingleTableEntityPersister;import org.hibernate.persister.entity.UnionSubclassEntityPersister;import org.hibernate.type.DiscriminatorType;import org.hibernate.type.ForeignKeyDirection;import org.hibernate.type.Type;import org.hibernate.type.TypeFactory;import org.hibernate.util.JoinedIterator;import org.hibernate.util.ReflectHelper;import org.hibernate.util.StringHelper;/** * Walks an XML mapping document and produces the Hibernate configuration-time metamodel (the * classes in the <tt>mapping</tt> package) * * @author Gavin King */public final class HbmBinder { private static final Logger log = LoggerFactory.getLogger( HbmBinder.class ); /** * Private constructor to disallow instantiation. */ private HbmBinder() { } /** * The main contract into the hbm.xml-based binder. Performs necessary binding operations * represented by the given DOM. * * @param doc The DOM to be parsed and bound. * @param mappings Current bind state. * @param inheritedMetas Any inherited meta-tag information. * @throws MappingException */ public static void bindRoot(Document doc, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { java.util.List names = HbmBinder.getExtendsNeeded( doc, mappings ); if ( !names.isEmpty() ) { // classes mentioned in extends not available - so put it in queue Element hmNode = doc.getRootElement(); Attribute packNode = hmNode.attribute( "package" ); String packageName = null; if ( packNode != null ) { packageName = packNode.getValue(); } Iterator itr = names.iterator(); while ( itr.hasNext() ) { String extendsName = (String) itr.next(); mappings.addToExtendsQueue( new ExtendsQueueEntry( extendsName, packageName, doc ) ); } return; } Element hmNode = doc.getRootElement(); // get meta's from <hibernate-mapping> inheritedMetas = getMetas( hmNode, inheritedMetas, true ); extractRootAttributes( hmNode, mappings ); Iterator rootChildren = hmNode.elementIterator(); while ( rootChildren.hasNext() ) { final Element element = (Element) rootChildren.next(); final String elementName = element.getName(); if ( "filter-def".equals( elementName ) ) { parseFilterDef( element, mappings ); } else if ( "typedef".equals( elementName ) ) { bindTypeDef( element, mappings ); } else if ( "class".equals( elementName ) ) { RootClass rootclass = new RootClass(); bindRootClass( element, rootclass, mappings, inheritedMetas ); mappings.addClass( rootclass ); } else if ( "subclass".equals( elementName ) ) { PersistentClass superModel = getSuperclass( mappings, element ); handleSubclass( superModel, mappings, element, inheritedMetas ); } else if ( "joined-subclass".equals( elementName ) ) { PersistentClass superModel = getSuperclass( mappings, element ); handleJoinedSubclass( superModel, mappings, element, inheritedMetas ); } else if ( "union-subclass".equals( elementName ) ) { PersistentClass superModel = getSuperclass( mappings, element ); handleUnionSubclass( superModel, mappings, element, inheritedMetas ); } else if ( "query".equals( elementName ) ) { bindNamedQuery( element, null, mappings ); } else if ( "sql-query".equals( elementName ) ) { bindNamedSQLQuery( element, null, mappings ); } else if ( "resultset".equals( elementName ) ) { bindResultSetMappingDefinition( element, null, mappings ); } else if ( "import".equals( elementName ) ) { bindImport( element, mappings ); } else if ( "database-object".equals( elementName ) ) { bindAuxiliaryDatabaseObject( element, mappings ); } } } private static void bindImport(Element importNode, Mappings mappings) { String className = getClassName( importNode.attribute( "class" ), mappings ); Attribute renameNode = importNode.attribute( "rename" ); String rename = ( renameNode == null ) ? StringHelper.unqualify( className ) : renameNode.getValue(); log.debug( "Import: " + rename + " -> " + className ); mappings.addImport( className, rename ); } private static void bindTypeDef(Element typedefNode, Mappings mappings) { String typeClass = typedefNode.attributeValue( "class" ); String typeName = typedefNode.attributeValue( "name" ); Iterator paramIter = typedefNode.elementIterator( "param" ); Properties parameters = new Properties(); while ( paramIter.hasNext() ) { Element param = (Element) paramIter.next(); parameters.setProperty( param.attributeValue( "name" ), param.getTextTrim() ); } mappings.addTypeDef( typeName, typeClass, parameters ); } private static void bindAuxiliaryDatabaseObject(Element auxDbObjectNode, Mappings mappings) { AuxiliaryDatabaseObject auxDbObject = null; Element definitionNode = auxDbObjectNode.element( "definition" ); if ( definitionNode != null ) { try { auxDbObject = ( AuxiliaryDatabaseObject ) ReflectHelper .classForName( definitionNode.attributeValue( "class" ) ) .newInstance(); } catch( ClassNotFoundException e ) { throw new MappingException( "could not locate custom database object class [" + definitionNode.attributeValue( "class" ) + "]" ); } catch( Throwable t ) { throw new MappingException( "could not instantiate custom database object class [" + definitionNode.attributeValue( "class" ) + "]" ); } } else { auxDbObject = new SimpleAuxiliaryDatabaseObject( auxDbObjectNode.elementTextTrim( "create" ), auxDbObjectNode.elementTextTrim( "drop" ) ); } Iterator dialectScopings = auxDbObjectNode.elementIterator( "dialect-scope" ); while ( dialectScopings.hasNext() ) { Element dialectScoping = ( Element ) dialectScopings.next(); auxDbObject.addDialectScope( dialectScoping.attributeValue( "name" ) ); } mappings.addAuxiliaryDatabaseObject( auxDbObject ); } private static void extractRootAttributes(Element hmNode, Mappings mappings) { Attribute schemaNode = hmNode.attribute( "schema" ); mappings.setSchemaName( ( schemaNode == null ) ? null : schemaNode.getValue() ); Attribute catalogNode = hmNode.attribute( "catalog" ); mappings.setCatalogName( ( catalogNode == null ) ? null : catalogNode.getValue() ); Attribute dcNode = hmNode.attribute( "default-cascade" ); mappings.setDefaultCascade( ( dcNode == null ) ? "none" : dcNode.getValue() ); Attribute daNode = hmNode.attribute( "default-access" ); mappings.setDefaultAccess( ( daNode == null ) ? "property" : daNode.getValue() ); Attribute dlNode = hmNode.attribute( "default-lazy" ); mappings.setDefaultLazy( dlNode == null || dlNode.getValue().equals( "true" ) ); Attribute aiNode = hmNode.attribute( "auto-import" ); mappings.setAutoImport( ( aiNode == null ) || "true".equals( aiNode.getValue() ) ); Attribute packNode = hmNode.attribute( "package" ); if ( packNode != null ) mappings.setDefaultPackage( packNode.getValue() ); } /** * Responsible for perfoming the bind operation related to an <class/> mapping element. * * @param node The DOM Element for the <class/> element. * @param rootClass The mapping instance to which to bind the information. * @param mappings The current bind state. * @param inheritedMetas Any inherited meta-tag information. * @throws MappingException */ public static void bindRootClass(Element node, RootClass rootClass, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { bindClass( node, rootClass, mappings, inheritedMetas ); inheritedMetas = getMetas( node, inheritedMetas, true ); // get meta's from <class> bindRootPersistentClassCommonValues( node, inheritedMetas, mappings, rootClass ); } private static void bindRootPersistentClassCommonValues(Element node, java.util.Map inheritedMetas, Mappings mappings, RootClass entity) throws MappingException { // DB-OBJECTNAME Attribute schemaNode = node.attribute( "schema" ); String schema = schemaNode == null ? mappings.getSchemaName() : schemaNode.getValue(); Attribute catalogNode = node.attribute( "catalog" ); String catalog = catalogNode == null ? mappings.getCatalogName() : catalogNode.getValue(); Table table = mappings.addTable( schema, catalog, getClassTableName( entity, node, schema, catalog, null, mappings ), getSubselect( node ), entity.isAbstract() != null && entity.isAbstract().booleanValue() ); entity.setTable( table ); bindComment(table, node); log.info( "Mapping class: " + entity.getEntityName() + " -> " + entity.getTable().getName() ); // MUTABLE Attribute mutableNode = node.attribute( "mutable" ); entity.setMutable( ( mutableNode == null ) || mutableNode.getValue().equals( "true" ) ); // WHERE Attribute whereNode = node.attribute( "where" ); if ( whereNode != null ) entity.setWhere( whereNode.getValue() ); // CHECK Attribute chNode = node.attribute( "check" ); if ( chNode != null ) table.addCheckConstraint( chNode.getValue() ); // POLYMORPHISM Attribute polyNode = node.attribute( "polymorphism" ); entity.setExplicitPolymorphism( ( polyNode != null ) && polyNode.getValue().equals( "explicit" ) ); // ROW ID Attribute rowidNode = node.attribute( "rowid" ); if ( rowidNode != null ) table.setRowId( rowidNode.getValue() ); Iterator subnodes = node.elementIterator(); while ( subnodes.hasNext() ) { Element subnode = (Element) subnodes.next(); String name = subnode.getName(); if ( "id".equals( name ) ) { // ID bindSimpleId( subnode, entity, mappings, inheritedMetas ); } else if ( "composite-id".equals( name ) ) { // COMPOSITE-ID bindCompositeId( subnode, entity, mappings, inheritedMetas ); } else if ( "version".equals( name ) || "timestamp".equals( name ) ) { // VERSION / TIMESTAMP bindVersioningProperty( table, subnode, mappings, name, entity, inheritedMetas ); } else if ( "discriminator".equals( name ) ) { // DISCRIMINATOR bindDiscriminatorProperty( table, entity, subnode, mappings ); } else if ( "cache".equals( name ) ) { entity.setCacheConcurrencyStrategy( subnode.attributeValue( "usage" ) ); entity.setCacheRegionName( subnode.attributeValue( "region" ) ); entity.setLazyPropertiesCacheable( !"non-lazy".equals( subnode.attributeValue( "include" ) ) ); } } // Primary key constraint entity.createPrimaryKey(); createClassProperties( node, entity, mappings, inheritedMetas ); } private static void bindSimpleId(Element idNode, RootClass entity, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { String propertyName = idNode.attributeValue( "name" ); SimpleValue id = new SimpleValue( entity.getTable() ); entity.setIdentifier( id ); // if ( propertyName == null || entity.getPojoRepresentation() == null ) { // bindSimpleValue( idNode, id, false, RootClass.DEFAULT_IDENTIFIER_COLUMN_NAME, mappings ); // if ( !id.isTypeSpecified() ) { // throw new MappingException( "must specify an identifier type: " + entity.getEntityName() // );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -