📄 binder.java
字号:
} //automatically makes a column with the default name if none is specifed by XML public static void bindSimpleValue(Element node, SimpleValue model, boolean isNullable, String defaultColumnName) throws MappingException { model.setType( getTypeFromXML(node) ); Attribute formulaNode = node.attribute("formula"); if (formulaNode!=null) { Formula f = new Formula(); f.setFormula( formulaNode.getText() ); model.setFormula(f); } else { bindColumns(node, model, isNullable, true, defaultColumnName); } Attribute fkNode = node.attribute("foreign-key"); if (fkNode!=null) model.setForeignKeyName( fkNode.getValue() ); } public static void bindProperty(Element node, Property model, Mappings mappings) throws MappingException { model.setName( getPropertyName(node) ); 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) model.setPropertyAccessorName( accessNode.getValue() ); Attribute cascadeNode = node.attribute("cascade"); model.setCascade( (cascadeNode==null) ? mappings.getDefaultCascade() : cascadeNode.getValue() ); Attribute updateNode = node.attribute("update"); model.setUpdateable( (updateNode==null) ? true : "true".equals( updateNode.getValue() ) ); Attribute insertNode = node.attribute("insert"); model.setInsertable( (insertNode==null) ? true : "true".equals( insertNode.getValue() ) ); if ( log.isDebugEnabled() ) { String msg = "Mapped property: " + model.getName(); String columns = columns( model.getValue() ); if ( columns.length() > 0 ) msg += " -> " + columns; if ( model.getType()!=null ) msg += ", type: " + model.getType().getName(); log.debug(msg); } model.setMetaAttributes( getMetas(node) ); } private static String columns(Value val) { StringBuffer columns = new StringBuffer(); Iterator iter = val.getColumnIterator(); while ( iter.hasNext() ) { columns.append( ( (Column) iter.next() ).getName() ); if ( iter.hasNext() ) columns.append(", "); } return columns.toString(); } /** * Called for all collections */ public static void bindCollection(Element node, Collection model, String prefix, Mappings mappings) throws MappingException { //ROLENAME String propertyName = node.attributeValue("name"); model.setRole( StringHelper.qualify(prefix, propertyName) ); Attribute inverseNode = node.attribute("inverse"); if ( inverseNode!=null) model.setInverse( StringHelper.booleanValue( inverseNode.getValue() ) ); Attribute orderNode = node.attribute("order-by"); if ( orderNode!=null) { if ( Environment.jvmSupportsLinkedHashCollections() || ( model instanceof Bag ) ) { model.setOrderBy( orderNode.getValue() ); } else { log.warn("Attribute \"order-by\" ignored in JDK1.3 or less"); } } Attribute whereNode = node.attribute("where"); if (whereNode!=null) { model.setWhere( whereNode.getValue() ); } Attribute batchNode = node.attribute("batch-size"); if (batchNode!=null) { model.setBatchSize( Integer.parseInt( batchNode.getValue() ) ); } //PERSISTER Attribute persisterNode = node.attribute("persister"); if (persisterNode==null) { //persister = CollectionPersisterImpl.class; } else { try { model.setCollectionPersisterClass( ReflectHelper.classForName( persisterNode.getValue() ) ); } catch (ClassNotFoundException cnfe) { throw new MappingException( "Could not find collection persister class: " + persisterNode.getValue() ); } } initOuterJoinFetchSetting(node, model); Element oneToManyNode = node.element("one-to-many"); if (oneToManyNode!=null) { OneToMany oneToMany = new OneToMany( model.getOwner() ); model.setElement(oneToMany); bindOneToMany(oneToManyNode, oneToMany); //we have to set up the table later!! yuck } else { //TABLE Attribute tableNode = node.attribute("table"); String tableName; if (tableNode!=null) { tableName = tableNode.getValue(); } else { tableName = propertyName; } Attribute schemaNode = node.attribute("schema"); String schema = schemaNode==null ? mappings.getSchemaName() : schemaNode.getValue(); model.setCollectionTable( mappings.addTable(schema, tableName) ); log.info("Mapping collection: " + model.getRole() + " -> " + model.getCollectionTable().getName() ); } //LAZINESS Attribute lazyNode = node.attribute("lazy"); if (lazyNode!=null) { model.setLazy( StringHelper.booleanValue( lazyNode.getValue() ) ); } //SORT Attribute sortedAtt = node.attribute("sort"); // unsorted, natural, comparator.class.name if ( sortedAtt==null || sortedAtt.getValue().equals("unsorted") ) { model.setSorted(false); } else { model.setSorted(true); String className = sortedAtt.getValue(); if ( !className.equals("natural") ) { try { model.setComparator( (Comparator) ReflectHelper.classForName(className).newInstance() ); } catch (Exception e) { throw new MappingException( "Could not instantiate comparator class: " + className ); } } } //ORPHAN DELETE (used for programmer error detection) Attribute cascadeAtt = node.attribute("cascade"); if ( cascadeAtt!=null && cascadeAtt.getValue().equals("all-delete-orphan") ) model.setOrphanDelete(true); //set up second pass if (model instanceof List) { mappings.addSecondPass( new ListSecondPass(node, mappings, (List) model) ); } else if (model instanceof Set) { mappings.addSecondPass( new SetSecondPass(node, mappings, (Set) model) ); } else if (model instanceof Map) { mappings.addSecondPass( new MapSecondPass(node, mappings, (Map) model) ); } else if (model instanceof IdentifierCollection) { mappings.addSecondPass( new IdentifierCollectionSecondPass(node, mappings, (IdentifierCollection) model) ); } else { mappings.addSecondPass( new CollectionSecondPass(node, mappings, model) ); } } public static void bindManyToOne(Element node, ManyToOne model, String defaultColumnName, boolean isNullable, Mappings mappings) throws MappingException { bindColumns(node, model, isNullable, true, defaultColumnName); initOuterJoinFetchSetting(node, model); Attribute ukName = node.attribute("property-ref"); if (ukName!=null) model.setReferencedPropertyName( ukName.getValue() ); Attribute typeNode = node.attribute("class"); if (typeNode!=null) { try { model.setType( TypeFactory.manyToOne( ReflectHelper.classForName( typeNode.getValue() ), model.getReferencedPropertyName() ) ); } catch (Exception e) { throw new MappingException( "Could not find class: " + typeNode.getValue() ); } } Attribute fkNode = node.attribute("foreign-key"); if (fkNode!=null) model.setForeignKeyName( node.attributeValue("foreign-key") ); } public static void bindAny(Element node, Any model, boolean isNullable) throws MappingException { model.setIdentifierType( getTypeFromXML(node) ); Attribute metaAttribute = node.attribute("meta-type"); if (metaAttribute!=null) { Type metaType = TypeFactory.heuristicType( metaAttribute.getValue() ); if ( metaType==null ) throw new MappingException("could not interpret meta-type"); model.setMetaType(metaType); } bindColumns(node, model, isNullable, false, null); } public static void bindOneToOne(Element node, OneToOne model, boolean isNullable, Mappings mappings) throws MappingException { bindColumns(node, model, isNullable, false, null); initOuterJoinFetchSetting(node, model); Attribute constrNode = node.attribute("constrained"); boolean constrained = constrNode!=null && constrNode.getValue().equals("true"); model.setConstrained(constrained); model.setForeignKeyType( constrained ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT ); Attribute fkNode = node.attribute("foreign-key"); if (fkNode!=null) model.setForeignKeyName( fkNode.getValue() ); Attribute ukName = node.attribute("property-ref"); if (ukName!=null) model.setReferencedPropertyName( ukName.getValue() ); Attribute typeNode = node.attribute("class"); if (typeNode!=null) { try { model.setType( TypeFactory.oneToOne( ReflectHelper.classForName( typeNode.getValue() ), model.getForeignKeyType(), model.getReferencedPropertyName() ) ); } catch (Exception e) { throw new MappingException( "Could not find class: " + typeNode.getValue() ); } } } public static void bindOneToMany(Element node, OneToMany model) throws MappingException { try { model.setType( (EntityType) Hibernate.entity( ReflectHelper.classForName( node.attributeValue("class") ) ) ); } catch (ClassNotFoundException cnfe) { throw new MappingException("Associated class not found", cnfe); } } public static void bindColumn(Element node, Column model, boolean isNullable) { Attribute lengthNode = node.attribute("length"); if (lengthNode!=null) model.setLength( Integer.parseInt( lengthNode.getValue() ) ); Attribute nullNode = node.attribute("not-null"); model.setNullable( (nullNode!=null) ? !StringHelper.booleanValue( nullNode.getValue() ) : isNullable ); Attribute unqNode = node.attribute("unique"); model.setUnique( unqNode!=null && StringHelper.booleanValue( unqNode.getValue() ) ); //Attribute qtNode = node.attribute("quote"); //model.setQuoted( qtNode!=null && StringHelper.booleanValue( qtNode.getValue() ) ); Attribute typeNode = node.attribute("sql-type"); model.setSqlType( (typeNode==null) ? null : typeNode.getValue() ); } /** * Called for arrays and primitive arrays */ public static void bindArray(Element node, Array model, String prefix, Mappings mappings) throws MappingException { bindCollection(node, model, prefix, mappings); Attribute att = node.attribute("element-class"); if ( att!=null ) { try { model.setElementClass( ReflectHelper.classForName( att.getValue() ) ); } catch (ClassNotFoundException cnfe) { throw new MappingException(cnfe); } } else { Iterator iter = node.elementIterator(); while ( iter.hasNext() ) { Element subnode = (Element) iter.next(); String name = subnode.getName(); if ( "element".equals(name) ) { Type type = getTypeFromXML(subnode); model.setElementClass( model.isPrimitiveArray() ? ( (PrimitiveType) type ).getPrimitiveClass() : type.getReturnedClass() ); } else if ( "one-to-many".equals(name) || "many-to-many".equals(name) || "composite-element".equals(name) ) { try { model.setElementClass( ReflectHelper.classForName( subnode.attributeValue("class") ) ); } catch (ClassNotFoundException cnfe) { throw new MappingException(cnfe); } } } } } public static void bindComponent(Element node, Component model, Class reflectedClass, String path, boolean isNullable, Mappings mappings) throws MappingException { Attribute classNode = node.attribute("class"); if ( "dynamic-component".equals( node.getName() ) ) { model.setEmbedded(false); model.setDynamic(true); } else if (classNode!=null) { try { model.setComponentClass( ReflectHelper.classForName( classNode.getValue() ) ); } catch (ClassNotFoundException cnfe) { throw new MappingException( "component class not found", cnfe ); } model.setEmbedded(false); } else if (reflectedClass!=null) { model.setComponentClass(reflectedClass); model.setEmbedded(false); } else { // an "embedded" component (ids only) model.setComponentClass( model.getOwner().getMappedClass() ); model.setEmbedded (true); } Iterator iter = node.elementIterator(); while ( iter.hasNext() ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -