?? binder.java
字號:
catch (ClassNotFoundException cnfe) { throw new MappingException(cnfe); } } } } } public static void bindComponent(Element node, Component model, Class reflectedClass, String className, 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( getClassName(classNode, mappings) ) ); } 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() ) { Element subnode = (Element) iter.next(); String name = subnode.getName(); String propertyName = subnode.attributeValue("name"); String subpath = propertyName==null ? null : StringHelper.qualify(path, propertyName); CollectionType collectType = CollectionType.collectionTypeFromString(name); Value value = null; if (collectType!=null) { Collection collection = collectType.create(subnode, className, subpath, model.getOwner(), mappings); mappings.addCollection(collection); value = collection; } else if ( "many-to-one".equals(name) || "key-many-to-one".equals(name) ) { value = new ManyToOne( model.getTable() ); bindManyToOne(subnode, (ManyToOne) value, subpath, isNullable, mappings); } else if ( "one-to-one".equals(name) ) { value = new OneToOne( model.getTable(), model.getOwner().getIdentifier() ); bindOneToOne(subnode, (OneToOne) value, isNullable, mappings); } else if ( "any".equals(name) ) { value = new Any( model.getTable() ); bindAny(subnode, (Any) value, isNullable, mappings); } else if ( "property".equals(name) || "key-property".equals(name) ) { value = new SimpleValue( model.getTable() ); bindSimpleValue(subnode, (SimpleValue) value, isNullable, subpath, mappings); } else if ( "component".equals(name) || "dynamic-component".equals(name) || "nested-composite-element".equals(name) ) { Class subreflectedClass = (model.getComponentClass()==null) ? null : ReflectHelper.reflectedPropertyClass( model.getComponentClass(), propertyName ); value = ( model.getOwner()!=null ) ? new Component( model.getOwner() ) : // a class component new Component( model.getTable() ); // a composite element bindComponent(subnode, (Component) value, subreflectedClass, className, subpath, isNullable, mappings); } else if ( "parent".equals(name) ) { model.setParentProperty(propertyName); } if ( value!=null) model.addProperty( createProperty(value, propertyName, model.getComponentClass(), subnode, mappings) ); } int span = model.getPropertySpan(); String[] names = new String[span]; Type[] types = new Type[span]; Cascades.CascadeStyle[] cascade = new Cascades.CascadeStyle[span]; int[] joinedFetch = new int[span]; iter = model.getPropertyIterator(); int i=0; while ( iter.hasNext() ) { Property prop = (Property) iter.next(); if ( prop.isFormula() ) { throw new MappingException( "properties of components may not be formulas: " + prop.getName() ); } if ( !prop.isInsertable() || !prop.isUpdateable() ) { throw new MappingException( "insert=\"false\", update=\"false\" not supported for properties of components: " + prop.getName() ); } names[i] = prop.getName(); types[i] = prop.getType(); cascade[i] = prop.getCascadeStyle(); joinedFetch[i] = prop.getValue().getOuterJoinFetchSetting(); i++; } final Type componentType; if ( model.isDynamic() ) { componentType = new DynamicComponentType(names, types, joinedFetch, cascade); } else { Getter[] getters = new Getter[span]; Setter[] setters = new Setter[span]; iter = model.getPropertyIterator(); boolean foundCustomAccessor=false; i=0; while ( iter.hasNext() ) { Property prop = (Property) iter.next(); setters[i] = prop.getSetter( model.getComponentClass() ); getters[i] = prop.getGetter( model.getComponentClass() ); if ( !prop.isBasicPropertyAccessor() ) foundCustomAccessor = true; i++; } componentType = new ComponentType( model.getComponentClass(), names, getters, setters, foundCustomAccessor, types, joinedFetch, cascade, model.getParentProperty() ); } model.setType(componentType); } private static Type getTypeFromXML(Element node) throws MappingException { Type type; Attribute typeNode = node.attribute("type"); if (typeNode==null) typeNode = node.attribute("id-type"); //for an any if (typeNode==null) { return null; //we will have to use reflection } else { type = TypeFactory.heuristicType( typeNode.getValue() ); if (type==null) throw new MappingException( "Could not interpret type: " + typeNode.getValue() ); } return type; } private static void initOuterJoinFetchSetting(Element node, Fetchable model) { Attribute jfNode = node.attribute("outer-join"); if ( jfNode==null ) { model.setOuterJoinFetchSetting(OuterJoinLoader.AUTO); } else { String eoj = jfNode.getValue(); if ( "auto".equals(eoj) ) { model.setOuterJoinFetchSetting(OuterJoinLoader.AUTO); } else { model.setOuterJoinFetchSetting( "true".equals(eoj) ? OuterJoinLoader.EAGER : OuterJoinLoader.LAZY ); } } } private static void makeIdentifier(Element node, SimpleValue model, Mappings mappings) { //GENERATOR Element subnode = node.element("generator"); if ( subnode!=null ) { model.setIdentifierGeneratorStrategy( subnode.attributeValue("class") ); Properties params = new Properties(); if ( mappings.getSchemaName()!=null ) { params.setProperty( PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName() ); } params.setProperty( PersistentIdentifierGenerator.TABLE, model.getTable().getName() ); params.setProperty( PersistentIdentifierGenerator.PK, ( (Column) model.getColumnIterator().next() ).getName() ); Iterator iter = subnode.elementIterator("param"); while( iter.hasNext() ) { Element childNode = (Element) iter.next(); params.setProperty( childNode.attributeValue("name"), childNode.getText() ); } model.setIdentifierGeneratorProperties(params); } model.getTable().setIdentifierValue(model); // ID UNSAVED-VALUE Attribute nullValueNode = node.attribute("unsaved-value"); if (nullValueNode!=null) { model.setNullValue( nullValueNode.getValue() ); } else { model.setNullValue("null"); } } private static final void makeVersion(Element node, SimpleValue model) { // VERSION UNSAVED-VALUE Attribute nullValueNode = node.attribute("unsaved-value"); if (nullValueNode!=null) { model.setNullValue( nullValueNode.getValue() ); } else { model.setNullValue("undefined"); } } protected static void propertiesFromXML(Element node, PersistentClass model, Mappings mappings) throws MappingException { Table table = model.getTable(); Iterator iter = node.elementIterator(); while( iter.hasNext() ) { Element subnode = (Element) iter.next(); String name = subnode.getName(); String propertyName = subnode.attributeValue("name"); CollectionType collectType = CollectionType.collectionTypeFromString(name); Value value = null; if (collectType!=null) { Collection collection = collectType.create(subnode, model.getName(), propertyName, model, mappings); mappings.addCollection(collection); value = collection; } else if ( "many-to-one".equals(name) ) { value = new ManyToOne(table); bindManyToOne(subnode, (ManyToOne) value, propertyName, true, mappings); } else if ( "any".equals(name) ) { value = new Any(table); bindAny(subnode, (Any) value, true, mappings); } else if ( "one-to-one".equals(name) ) { OneToOne oneToOne = new OneToOne( table, model.getIdentifier() ); bindOneToOne(subnode, oneToOne, true, mappings); value = oneToOne; } else if ( "property".equals(name) ) { value = new SimpleValue(table); bindSimpleValue(subnode, (SimpleValue) value, true, propertyName, mappings); } else if ( "component".equals(name) || "dynamic-component".equals(name) ) { Class reflectedClass = ReflectHelper.reflectedPropertyClass( model.getMappedClass(), propertyName ); value = new Component(model); bindComponent(subnode, (Component) value, reflectedClass, model.getName(), propertyName, true, mappings); } else if ( "subclass".equals(name) ) { handleSubclass(model, mappings, subnode); } else if ( "joined-subclass".equals(name) ) { handleJoinedSubclass(model, mappings, subnode); } if ( value!=null) model.addNewProperty( createProperty(value, propertyName, model.getMappedClass(), subnode, mappings) ); } } private static Property createProperty(Value value, String propertyName, Class parentClass, Element subnode, Mappings mappings) throws MappingException { if ( parentClass!=null && value.isSimpleValue() ) ( (SimpleValue) value ).setTypeByReflection(parentClass, propertyName); if ( value instanceof ToOne ) { //this is done here 'cos we might only know the type here (ugly!) String propertyRef = ( (ToOne) value ).getReferencedPropertyName(); if (propertyRef!=null) mappings.addUniquePropertyReference( ( (EntityType) value.getType() ).getAssociatedClass(), propertyRef ); } value.createForeignKey(); Property prop = new Property(value); bindProperty(subnode, prop, mappings); return prop; } private static void handleJoinedSubclass(PersistentClass model, Mappings mappings, Element subnode) throws MappingException { Subclass subclass = new Subclass(model); bindJoinedSubclass( subnode, subclass, mappings ); model.addSubclass(subclass); mappings.addClass(subclass); } private static void handleSubclass(PersistentClass model, Mappings mappings, Element subnode) throws MappingException { Subclass subclass = new Subclass(model); bindSubclass( subnode, subclass, mappings ); model.addSubclass(subclass); mappings.addClass(subclass); } /** * Called for Lists, arrays, primitive arrays */ public static void bindListSecondPass(Element node, List model, java.util.Map classes, Mappings mappings) throws MappingException { bindCollectionSecondPass(node, model, classes, mappings); Element subnode = node.element("index"); SimpleValue iv = new SimpleValue( model.getCollectionTable() ); bindSimpleValue(subnode, iv, model.isOneToMany(), IndexedCollection.DEFAULT_INDEX_COLUMN_NAME, mappings); iv.setType(Hibernate.INTEGER); model.setIndex(iv); } public static void bindIdentifierCollectionSecondPass(Element node, IdentifierCollection model, java.util.Map persistentClasses, Mappings mappings) throws MappingException { bindCollectionSecondPass(node, model, persistentClasses, mappings); Element subnode = node.element("collection-id"); SimpleValue id = new SimpleValue( model.getCollectionTable() ); bindSimpleValue(subnode, id, false, IdentifierCollection.DEFAULT_IDENTIFIER_COLUMN_NAME, mappings); model.setIdentifier(id); makeIdentifier(subnode, id, mappings); } /** * Called for Maps */ public static void bindMapSecondPass(Element node, Map model, java.util.Map classes, Mappings mappings) throws MappingException { bindCollectionSecondPass(node, model, classes, mappings); Iterator iter = node.elementIterator(); while( iter.hasNext() ) { Element subnode = (Element) iter.next(); String name = subnode.getName(); if ( "index".equals(name) ) { SimpleValue value = new SimpleValue( model.getCollectionTable() ); bindSimpleValue(subnode, value, model.isOneToMany(), IndexedCollection.DEFAULT_INDEX_COLUMN_NAME, mappings); model.setIndex(value); if ( model.getIndex().getType()==null ) throw new MappingException("map index element must specify a type"); } else if ( "index-many-to-many".equals(name) ) { ManyToOne mto = new ManyToOne( model.getCollectionTable() ); bindManyToOne( subnode, mto, IndexedCollection.DEFAULT_INDEX_COLUMN_NAME, model.isOneToMany(), mappings ); model.setIndex(mto); } else if ( "composite-index".equals(name) ) { Component component = new Component( model.getCollectionTable() ); bindComponent(subnode, component, null, model.getRole(), "index", model.isOneToMany(), mappings); model.setIndex(component); } else if ( "index-many-to-any".equals(name) ) { Any any = new Any( model.getCollectionTable() ); bindAny( subnode, any, model.isOneToMany(), mappings ); model.setIndex(any); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -