?? binder.java
字號:
//$Id: Binder.java,v 1.26.2.60 2004/01/29 02:11:58 oneovthafew Exp $package net.sf.hibernate.cfg;import java.io.Serializable;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.Properties;import net.sf.hibernate.Hibernate;import net.sf.hibernate.HibernateException;import net.sf.hibernate.MappingException;import net.sf.hibernate.mapping.IdentifierBag;import net.sf.hibernate.cache.CacheConcurrencyStrategy;import net.sf.hibernate.cache.CacheFactory;import net.sf.hibernate.engine.Cascades;import net.sf.hibernate.engine.Versioning;import net.sf.hibernate.id.PersistentIdentifierGenerator;import net.sf.hibernate.loader.OuterJoinLoader;import net.sf.hibernate.mapping.Any;import net.sf.hibernate.mapping.Array;import net.sf.hibernate.mapping.MetaAttribute;import net.sf.hibernate.mapping.Value;import net.sf.hibernate.mapping.ToOne;import net.sf.hibernate.mapping.Bag;import net.sf.hibernate.mapping.Collection;import net.sf.hibernate.mapping.Column;import net.sf.hibernate.mapping.Component;import net.sf.hibernate.mapping.Fetchable;import net.sf.hibernate.mapping.Formula;import net.sf.hibernate.mapping.IdentifierCollection;import net.sf.hibernate.mapping.IndexedCollection;import net.sf.hibernate.mapping.List;import net.sf.hibernate.mapping.ManyToOne;import net.sf.hibernate.mapping.Map;import net.sf.hibernate.mapping.NamedSQLQuery;import net.sf.hibernate.mapping.OneToMany;import net.sf.hibernate.mapping.OneToOne;import net.sf.hibernate.mapping.PersistentClass;import net.sf.hibernate.mapping.PrimitiveArray;import net.sf.hibernate.mapping.Property;import net.sf.hibernate.mapping.RootClass;import net.sf.hibernate.mapping.Set;import net.sf.hibernate.mapping.Subclass;import net.sf.hibernate.mapping.Table;import net.sf.hibernate.mapping.SimpleValue;import net.sf.hibernate.persister.EntityPersister;import net.sf.hibernate.persister.NormalizedEntityPersister;import net.sf.hibernate.property.Getter;import net.sf.hibernate.property.Setter;import net.sf.hibernate.type.ComponentType;import net.sf.hibernate.type.DiscriminatorType;import net.sf.hibernate.type.DynamicComponentType;import net.sf.hibernate.type.EntityType;import net.sf.hibernate.type.ForeignKeyDirection;import net.sf.hibernate.type.MetaType;import net.sf.hibernate.type.PrimitiveType;import net.sf.hibernate.type.Type;import net.sf.hibernate.type.TypeFactory;import net.sf.hibernate.util.ReflectHelper;import net.sf.hibernate.util.StringHelper;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.Element;final class Binder { private Binder() {} private static final Log log = LogFactory.getLog(Binder.class); public static void bindClass(Element node, PersistentClass model, Mappings mapping) throws MappingException { //CLASS String className = getClassName( node.attribute("name"), mapping ); try { model.setMappedClass( ReflectHelper.classForName(className) ); } catch ( ClassNotFoundException cnfe ) { throw new MappingException( "persistent class [" + className + "] not found", cnfe ); } //PROXY INTERFACE Attribute proxyNode = node.attribute("proxy"); Attribute lazyNode = node.attribute("lazy"); boolean lazyTrue = lazyNode!=null && "true".equals( lazyNode.getValue() ); if ( proxyNode!=null && ( lazyNode==null || lazyTrue ) ) { try { model.setProxyInterface( ReflectHelper.classForName( getClassName(proxyNode, mapping) ) ); } catch (ClassNotFoundException cnfe) { throw new MappingException(cnfe); } } if ( proxyNode==null && lazyTrue ) { model.setProxyInterface( model.getMappedClass() ); } //DISCRIMINATOR Attribute discriminatorNode = node.attribute("discriminator-value"); model.setDiscriminatorValue( (discriminatorNode==null) ? model.getName() : discriminatorNode.getValue() ); //DYNAMIC UPDATE Attribute dynamicNode = node.attribute("dynamic-update"); model.setDynamicUpdate( (dynamicNode==null) ? false : "true".equals( dynamicNode.getValue() ) ); //DYNAMIC UPDATE Attribute insertNode = node.attribute("dynamic-insert"); model.setDynamicInsert( (insertNode==null) ? false : "true".equals( insertNode.getValue() ) ); //IMPORT if ( mapping.isAutoImport() ) { mapping.addImport( className, StringHelper.unqualify(className) ); } //BATCH SIZE Attribute batchNode = node.attribute("batch-size"); if (batchNode!=null) model.setBatchSize( Integer.parseInt( batchNode.getValue() ) ); //SELECT BEFORE UPDATE Attribute sbuNode = node.attribute("select-before-update"); if (sbuNode!=null) model.setSelectBeforeUpdate( "true".equals( sbuNode.getValue() ) ); //OPTIMISTIC LOCK MODE Attribute olNode = node.attribute("optimistic-lock"); model.setOptimisticLockMode( getOptimisticLockMode(olNode) ); model.setMetaAttributes( getMetas(node) ); //PERSISTER Attribute persisterNode = node.attribute("persister"); if (persisterNode==null) { //persister = EntityPersister.class; } else { try { model.setClassPersisterClass( ReflectHelper.classForName( persisterNode.getValue() ) ); } catch (ClassNotFoundException cnfe) { throw new MappingException( "Could not find persister class: " + persisterNode.getValue() ); } } } public static void bindSubclass(Element node, Subclass model, Mappings mappings) throws MappingException { bindClass(node, model, mappings); if ( model.getClassPersisterClass()==null ) { model.getRootClass().setClassPersisterClass(EntityPersister.class); } model.setTable( model.getSuperclass().getTable() ); log.info("Mapping subclass: " + model.getName() + " -> " + model.getTable().getName() ); // properties propertiesFromXML(node, model, mappings); } private static String getClassTableName(PersistentClass model, Element node, Mappings mappings) { Attribute tableNameNode = node.attribute("table"); if (tableNameNode==null) { return mappings.getNamingStrategy().classToTableName( model.getName() ); } else { return mappings.getNamingStrategy().tableName( tableNameNode.getValue() ); } } public static void bindJoinedSubclass(Element node, Subclass model, Mappings mappings) throws MappingException { bindClass(node, model, mappings); // joined subclasses if ( model.getClassPersisterClass()==null ) { model.getRootClass().setClassPersisterClass(NormalizedEntityPersister.class); } // table + schema names Attribute schemaNode = node.attribute("schema"); String schema = schemaNode==null ? mappings.getSchemaName() : schemaNode.getValue(); Table mytable = mappings.addTable( schema, getClassTableName(model, node, mappings) ); model.setTable(mytable); log.info("Mapping joined-subclass: " + model.getName() + " -> " + model.getTable().getName() ); Element keyNode = node.element("key"); SimpleValue key = new SimpleValue(mytable); model.setKey(key); bindSimpleValue(keyNode, key, false, model.getName(), mappings); model.getKey().setType( model.getIdentifier().getType() ); model.createPrimaryKey(); model.createForeignKey(); //CHECK Attribute chNode = node.attribute("check"); if (chNode!=null) mytable.addCheckConstraint( chNode.getValue() ); // properties propertiesFromXML(node, model, mappings); } public static void bindRootClass(Element node, RootClass model, Mappings mappings) throws MappingException { bindClass(node, model, mappings); //TABLENAME Attribute schemaNode = node.attribute("schema"); String schema = schemaNode==null ? mappings.getSchemaName() : schemaNode.getValue(); Table table = mappings.addTable( schema, getClassTableName(model, node, mappings) ); model.setTable(table); log.info("Mapping class: " + model.getName() + " -> " + model.getTable().getName() ); //MUTABLE Attribute mutableNode = node.attribute("mutable"); model.setMutable( (mutableNode==null) || mutableNode.getValue().equals("true") ); //WHERE Attribute whereNode = node.attribute("where"); if (whereNode!=null) model.setWhere( whereNode.getValue() ); //CHECK Attribute chNode = node.attribute("check"); if (chNode!=null) table.addCheckConstraint( chNode.getValue() ); //POLYMORPHISM Attribute polyNode = node.attribute("polymorphism"); model.setExplicitPolymorphism( (polyNode!=null) && polyNode.getValue().equals("explicit") ); Iterator subnodes = node.elementIterator(); while ( subnodes.hasNext() ) { Element subnode = (Element) subnodes.next(); String name = subnode.getName(); String propertyName = subnode.attributeValue("name"); if ( "id".equals(name) ) { SimpleValue id = new SimpleValue(table); model.setIdentifier(id); if (propertyName==null) { bindSimpleValue(subnode, id, false, RootClass.DEFAULT_IDENTIFIER_COLUMN_NAME, mappings); if (id.getType()==null) throw new MappingException( "must specify an identifier type: " + model.getMappedClass().getName() ); model.setIdentifierProperty(null); } else { bindSimpleValue(subnode, id, false, propertyName, mappings); id.setTypeByReflection( model.getMappedClass(), propertyName ); Property prop = new Property(id); bindProperty(subnode, prop, mappings); model.setIdentifierProperty(prop); } if ( id.getType().getReturnedClass().isArray() ) throw new MappingException( "illegal use of an array as an identifier (arrays don't reimplement equals)" ); makeIdentifier(subnode, id, mappings); } else if ( "composite-id".equals(name) ) { Component id = new Component(model); model.setIdentifier(id); if (propertyName==null) { bindComponent(subnode, id, null, model.getName(), "id", false, mappings); model.setEmbeddedIdentifier( id.isEmbedded() ); model.setIdentifierProperty(null); } else { Class reflectedClass = ReflectHelper.reflectedPropertyClass( model.getMappedClass(), propertyName ); bindComponent( subnode, id, reflectedClass, model.getName(), propertyName, false, mappings ); Property prop = new Property(id); bindProperty(subnode, prop, mappings); model.setIdentifierProperty(prop); } makeIdentifier(subnode, id, mappings); Class idClass = id.getComponentClass(); if ( !ReflectHelper.overridesEquals(idClass) ) { throw new MappingException( "composite-id class must override equals() and hashCode(): " + id.getComponentClass().getName() ); } if ( !Serializable.class.isAssignableFrom(idClass) ) { throw new MappingException( "composite-id class must implement Serializable: " + id.getComponentClass().getName() ); } } else if ( "version".equals(name) || "timestamp".equals(name) ) { //VERSION SimpleValue val = new SimpleValue(table); bindSimpleValue(subnode, val, false, propertyName, mappings); if ( val.getType()==null ) val.setType( "version".equals(name) ? Hibernate.INTEGER : Hibernate.TIMESTAMP ); Property prop = new Property(val); bindProperty(subnode, prop, mappings); makeVersion(subnode, val); model.setVersion(prop); model.addNewProperty(prop); } else if ( "discriminator".equals(name) ) { //DISCRIMINATOR SimpleValue discrim = new SimpleValue(table); model.setDiscriminator(discrim); bindSimpleValue(subnode, discrim, false, RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, mappings); if ( discrim.getType()==null ) { discrim.setType(Hibernate.STRING); ( (Column) discrim.getColumnIterator().next() ).setType(Hibernate.STRING); } model.setPolymorphic(true); if ( "true".equals( subnode.attributeValue("force") ) ) model.setForceDiscriminator(true); } else if ( "jcs-cache".equals(name) || "cache".equals(name) ) { String className = model.getMappedClass().getName(); CacheConcurrencyStrategy cache = CacheFactory.createCache( subnode, className, model.isMutable() ); mappings.addCache(className, cache); model.setCache(cache); } } //Primary key constraint model.createPrimaryKey(); propertiesFromXML(node, model, mappings); } public static void bindColumns( final Element node, final SimpleValue model, final boolean isNullable, final boolean autoColumn, final String propertyPath, final Mappings mappings) { //COLUMN(S) Attribute columnAttribute = node.attribute("column"); if ( columnAttribute==null) { Iterator iter = node.elementIterator("column"); int count=0; while( iter.hasNext() ) { Element columnElement = (Element) iter.next(); Table table = model.getTable(); Column col = new Column( model.getType(), count++ ); bindColumn(columnElement, col, isNullable); col.setName( mappings.getNamingStrategy().columnName( columnElement.attributeValue("name") ) ); if (table!=null) table.addColumn(col); //table=null -> an association - fill it in later model.addColumn(col); //column index Attribute indexNode = columnElement.attribute("index");
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -