📄 hbmbinder.java
字号:
String path, Mappings mappings) throws MappingException { bindSimpleValueType( node, simpleValue, mappings ); bindColumnsOrFormula( node, simpleValue, path, isNullable, mappings ); Attribute fkNode = node.attribute( "foreign-key" ); if ( fkNode != null ) simpleValue.setForeignKeyName( fkNode.getValue() ); } private static void bindSimpleValueType(Element node, SimpleValue simpleValue, Mappings mappings) throws MappingException { String typeName = null; Properties parameters = new Properties(); Attribute typeNode = node.attribute( "type" ); if ( typeNode == null ) typeNode = node.attribute( "id-type" ); // for an any if ( typeNode != null ) typeName = typeNode.getValue(); Element typeChild = node.element( "type" ); if ( typeName == null && typeChild != null ) { typeName = typeChild.attribute( "name" ).getValue(); Iterator typeParameters = typeChild.elementIterator( "param" ); while ( typeParameters.hasNext() ) { Element paramElement = (Element) typeParameters.next(); parameters.setProperty( paramElement.attributeValue( "name" ), paramElement.getTextTrim() ); } } TypeDef typeDef = mappings.getTypeDef( typeName ); if ( typeDef != null ) { typeName = typeDef.getTypeClass(); // parameters on the property mapping should // override parameters in the typedef Properties allParameters = new Properties(); allParameters.putAll( typeDef.getParameters() ); allParameters.putAll( parameters ); parameters = allParameters; } if ( !parameters.isEmpty() ) simpleValue.setTypeParameters( parameters ); if ( typeName != null ) simpleValue.setTypeName( typeName ); } public static void bindProperty( Element node, Property property, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { String propName = node.attributeValue( "name" ); property.setName( propName ); String nodeName = node.attributeValue( "node" ); if (nodeName==null) nodeName = propName; property.setNodeName( nodeName ); // TODO: //Type type = model.getValue().getType(); //if (type==null) throw new MappingException( //"Could not determine a property type for: " + model.getName() ); Attribute accessNode = node.attribute( "access" ); if ( accessNode != null ) { property.setPropertyAccessorName( accessNode.getValue() ); } else if ( node.getName().equals( "properties" ) ) { property.setPropertyAccessorName( "embedded" ); } else { property.setPropertyAccessorName( mappings.getDefaultAccess() ); } Attribute cascadeNode = node.attribute( "cascade" ); property.setCascade( cascadeNode == null ? mappings.getDefaultCascade() : cascadeNode .getValue() ); Attribute updateNode = node.attribute( "update" ); property.setUpdateable( updateNode == null || "true".equals( updateNode.getValue() ) ); Attribute insertNode = node.attribute( "insert" ); property.setInsertable( insertNode == null || "true".equals( insertNode.getValue() ) ); Attribute lockNode = node.attribute( "optimistic-lock" ); property.setOptimisticLocked( lockNode == null || "true".equals( lockNode.getValue() ) ); Attribute generatedNode = node.attribute( "generated" ); String generationName = generatedNode == null ? null : generatedNode.getValue(); PropertyGeneration generation = PropertyGeneration.parse( generationName ); property.setGeneration( generation ); if ( generation == PropertyGeneration.ALWAYS || generation == PropertyGeneration.INSERT ) { // generated properties can *never* be insertable... if ( property.isInsertable() ) { if ( insertNode == null ) { // insertable simply because that is the user did not specify // anything; just override it property.setInsertable( false ); } else { // the user specifically supplied insert="true", // which constitutes an illegal combo throw new MappingException( "cannot specify both insert=\"true\" and generated=\"" + generation.getName() + "\" for property: " + propName ); } } // properties generated on update can never be updateable... if ( property.isUpdateable() && generation == PropertyGeneration.ALWAYS ) { if ( updateNode == null ) { // updateable only because the user did not specify // anything; just override it property.setUpdateable( false ); } else { // the user specifically supplied update="true", // which constitutes an illegal combo throw new MappingException( "cannot specify both update=\"true\" and generated=\"" + generation.getName() + "\" for property: " + propName ); } } } boolean isLazyable = "property".equals( node.getName() ) || "component".equals( node.getName() ) || "many-to-one".equals( node.getName() ) || "one-to-one".equals( node.getName() ) || "any".equals( node.getName() ); if ( isLazyable ) { Attribute lazyNode = node.attribute( "lazy" ); property.setLazy( lazyNode != null && "true".equals( lazyNode.getValue() ) ); } if ( log.isDebugEnabled() ) { String msg = "Mapped property: " + property.getName(); String columns = columns( property.getValue() ); if ( columns.length() > 0 ) msg += " -> " + columns; // TODO: this fails if we run with debug on! // if ( model.getType()!=null ) msg += ", type: " + model.getType().getName(); log.debug( msg ); } property.setMetaAttributes( getMetas( node, inheritedMetas ) ); } private static String columns(Value val) { StringBuffer columns = new StringBuffer(); Iterator iter = val.getColumnIterator(); while ( iter.hasNext() ) { columns.append( ( (Selectable) iter.next() ).getText() ); if ( iter.hasNext() ) columns.append( ", " ); } return columns.toString(); } /** * Called for all collections */ public static void bindCollection(Element node, Collection collection, String className, String path, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { // ROLENAME collection.setRole(path); Attribute inverseNode = node.attribute( "inverse" ); if ( inverseNode != null ) { collection.setInverse( "true".equals( inverseNode.getValue() ) ); } Attribute mutableNode = node.attribute( "mutable" ); if ( mutableNode != null ) { collection.setMutable( !"false".equals( mutableNode.getValue() ) ); } Attribute olNode = node.attribute( "optimistic-lock" ); collection.setOptimisticLocked( olNode == null || "true".equals( olNode.getValue() ) ); Attribute orderNode = node.attribute( "order-by" ); if ( orderNode != null ) { if ( Environment.jvmSupportsLinkedHashCollections() || ( collection instanceof Bag ) ) { collection.setOrderBy( orderNode.getValue() ); } else { log.warn( "Attribute \"order-by\" ignored in JDK1.3 or less" ); } } Attribute whereNode = node.attribute( "where" ); if ( whereNode != null ) { collection.setWhere( whereNode.getValue() ); } Attribute batchNode = node.attribute( "batch-size" ); if ( batchNode != null ) { collection.setBatchSize( Integer.parseInt( batchNode.getValue() ) ); } String nodeName = node.attributeValue( "node" ); if ( nodeName == null ) nodeName = node.attributeValue( "name" ); collection.setNodeName( nodeName ); String embed = node.attributeValue( "embed-xml" ); collection.setEmbedded( embed==null || "true".equals(embed) ); // PERSISTER Attribute persisterNode = node.attribute( "persister" ); if ( persisterNode != null ) { try { collection.setCollectionPersisterClass( ReflectHelper.classForName( persisterNode .getValue() ) ); } catch (ClassNotFoundException cnfe) { throw new MappingException( "Could not find collection persister class: " + persisterNode.getValue() ); } } Attribute typeNode = node.attribute( "collection-type" ); if ( typeNode != null ) { String typeName = typeNode.getValue(); TypeDef typeDef = mappings.getTypeDef( typeName ); if ( typeDef != null ) { collection.setTypeName( typeDef.getTypeClass() ); collection.setTypeParameters( typeDef.getParameters() ); } else { collection.setTypeName( typeName ); } } // FETCH STRATEGY initOuterJoinFetchSetting( node, collection ); if ( "subselect".equals( node.attributeValue("fetch") ) ) { collection.setSubselectLoadable(true); collection.getOwner().setSubselectLoadableCollections(true); } initLaziness( node, collection, mappings, "true", mappings.isDefaultLazy() ); //TODO: suck this into initLaziness! if ( "extra".equals( node.attributeValue("lazy") ) ) { collection.setLazy(true); collection.setExtraLazy(true); } Element oneToManyNode = node.element( "one-to-many" ); if ( oneToManyNode != null ) { OneToMany oneToMany = new OneToMany( collection.getOwner() ); collection.setElement( oneToMany ); bindOneToMany( oneToManyNode, oneToMany, mappings ); // we have to set up the table later!! yuck } else { // TABLE Attribute tableNode = node.attribute( "table" ); String tableName; if ( tableNode != null ) { tableName = mappings.getNamingStrategy().tableName( tableNode.getValue() ); } else { //tableName = mappings.getNamingStrategy().propertyToTableName( className, path ); Table ownerTable = collection.getOwner().getTable(); //TODO mappings.getLogicalTableName(ownerTable) String logicalOwnerTableName = ownerTable.getName(); //FIXME we don't have the associated entity table name here, has to be done in a second pass tableName = mappings.getNamingStrategy().collectionTableName( collection.getOwner().getEntityName(), logicalOwnerTableName , null, null, path ); } 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, tableName, getSubselect( node ), false ); collection.setCollectionTable( table ); bindComment(table, node); log.info( "Mapping collection: " + collection.getRole() + " -> " + collection.getCollectionTable().getName() ); } // SORT Attribute sortedAtt = node.attribute( "sort" ); // unsorted, natural, comparator.class.name if ( sortedAtt == null || sortedAtt.getValue().equals( "unsorted" ) ) { collection.setSorted( false ); } else { collection.setSorted( true ); String comparatorClassName = sortedAtt.getValue(); if ( !comparatorClassName.equals( "natural" ) ) { collection.setComparatorClassName(comparatorClassName); } } // ORPHAN DELETE (used for programmer error detection) Attribute cascadeAtt = node.attribute( "cascade" ); if ( cascadeAtt != null && cascadeAtt.getValue().indexOf( "delete-orphan" ) >= 0 ) { collection.setOrphanDelete( true ); } // CUSTOM SQL handleCustomSQL( node, collection ); // set up second pass if ( collection instanceof List ) { mappings.addSecondPass( new ListSecondPass( node, mappings, (List) collection, inheritedMetas ) ); } else if ( collection instanceof Map ) { mappings.addSecondPass( new MapSecondPass( node, mappings, (Map) collection, inheritedMetas ) ); } else if ( collection instanceof IdentifierCollection ) { mappings.addSecondPass( new IdentifierCollectionSecondPass( node, mappings, collection, inheritedMetas ) ); } else { mappings.addSecondPass( new CollectionSecondPass( node, mappings, collection, inheritedMetas ) ); } Iterator iter = node.elementIterator( "filter" ); while ( iter.hasNext() ) { final Element filter = (Element) iter.next(); parseFilter( filter, collection, mappings ); } Iterator tables = node.elementIterator( "synchronize" ); while ( tables.hasNext() ) { collection.getSynchronizedTables().add( ( (Element) tables.next() ).attributeValue( "table" ) ); } Element element = node.element( "loader" ); if ( element != null ) { collection.setLoaderName( element.attributeValue( "query-ref" ) ); } collection.setReferencedPropertyName( node.element( "key" ).attributeValue( "property-ref" ) ); } private static void initLaziness( Element node, Fetchable fetchable, Mappings mappings,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -