?? binder.java
字號:
} } /** * Called for all collections */ public static void bindCollectionSecondPass(Element node, Collection model, java.util.Map persistentClasses, Mappings mappings) throws MappingException { if ( model.isOneToMany() ) { OneToMany oneToMany = (OneToMany) model.getElement(); Class assocClass = oneToMany.getEntityType().getAssociatedClass(); PersistentClass persistentClass = (PersistentClass) persistentClasses.get(assocClass); if (persistentClass==null) throw new MappingException( "Association references unmapped class: " + assocClass.getName() ); oneToMany.setAssociatedClass(persistentClass); model.setCollectionTable( persistentClass.getTable() ); log.info("Mapping collection: " + model.getRole() + " -> " + model.getCollectionTable().getName() ); } //CHECK Attribute chNode = node.attribute("check"); if (chNode!=null) { model.getCollectionTable().addCheckConstraint( chNode.getValue() ); } //contained elements: Iterator iter = node.elementIterator(); while( iter.hasNext() ) { Element subnode = (Element) iter.next(); String name = subnode.getName(); if ( "key".equals(name) ) { SimpleValue key = new SimpleValue( model.getCollectionTable() ); bindSimpleValue(subnode, key, model.isOneToMany(), Collection.DEFAULT_KEY_COLUMN_NAME, mappings); key.setType( model.getOwner().getIdentifier().getType() ); if ( key.getType().getReturnedClass().isArray() ) throw new MappingException( "illegal use of an array as an identifier (arrays don't reimplement equals)" ); model.setKey(key); } else if ( "element".equals(name) ) { SimpleValue elt = new SimpleValue( model.getCollectionTable() ); model.setElement(elt); bindSimpleValue(subnode, elt, true, Collection.DEFAULT_ELEMENT_COLUMN_NAME, mappings); } else if ( "many-to-many".equals(name) ) { ManyToOne element = new ManyToOne( model.getCollectionTable() ); model.setElement(element); bindManyToOne(subnode, element, Collection.DEFAULT_ELEMENT_COLUMN_NAME, false, mappings); } else if ( "composite-element".equals(name) ) { Component element = new Component( model.getCollectionTable() ); model.setElement(element); bindComponent(subnode, element, null, model.getRole(), "element", true, mappings); } else if ( "many-to-any".equals(name) ) { Any element = new Any( model.getCollectionTable() ); model.setElement(element); bindAny(subnode, element, true, mappings); } else if ( "jcs-cache".equals(name) || "cache".equals(name) ) { CacheConcurrencyStrategy cache = CacheFactory.createCache( subnode, model.getRole(), model.getOwner().isMutable() ); mappings.addCache( model.getRole(), cache ); model.setCache(cache); } } } public static void bindRoot(Document doc, Mappings model) throws MappingException { Element hmNode = doc.getRootElement(); Attribute schemaNode = hmNode.attribute("schema"); model.setSchemaName( (schemaNode==null) ? null : schemaNode.getValue() ); Attribute dcNode = hmNode.attribute("default-cascade"); model.setDefaultCascade( (dcNode==null) ? "none" : dcNode.getValue() ); Attribute daNode = hmNode.attribute("default-access"); model.setDefaultAccess( (daNode==null) ? "property" : daNode.getValue() ); Attribute aiNode = hmNode.attribute("auto-import"); model.setAutoImport( (aiNode==null) ? true : "true".equals( aiNode.getValue() ) ); Attribute packNode = hmNode.attribute("package"); if (packNode!=null) model.setDefaultPackage( packNode.getValue() ); Iterator nodes = hmNode.elementIterator("class"); while ( nodes.hasNext() ) { Element n = (Element) nodes.next(); RootClass rootclass = new RootClass(); Binder.bindRootClass(n, rootclass, model); model.addClass(rootclass); } Iterator subclassnodes = hmNode.elementIterator("subclass"); while ( subclassnodes.hasNext() ) { Element subnode = (Element) subclassnodes.next(); PersistentClass superModel = getSuperclass(model, subnode); handleSubclass(superModel, model, subnode); } Iterator joinedsubclassnodes = hmNode.elementIterator("joined-subclass"); while ( joinedsubclassnodes.hasNext() ) { Element subnode = (Element) joinedsubclassnodes.next(); PersistentClass superModel = getSuperclass(model, subnode); handleJoinedSubclass(superModel, model, subnode); } nodes = hmNode.elementIterator("query"); while ( nodes.hasNext() ) { Element n = (Element) nodes.next(); String qname = n.attributeValue("name"); String query = n.getText(); log.debug("Named query: " + qname + " -> " + query); model.addQuery(qname, query); } nodes = hmNode.elementIterator("sql-query"); while ( nodes.hasNext() ) { Element queryElem = (Element) nodes.next(); String queryName = queryElem.attribute("name").getValue(); NamedSQLQuery namedQuery = new NamedSQLQuery( queryElem.getText() ); Iterator returns = queryElem.elementIterator("return"); while ( returns.hasNext() ) { Element returnElem = (Element) returns.next(); String alias = returnElem.attribute("alias").getText(); Class clazz; try { clazz = ReflectHelper.classForName( getClassName( returnElem.attribute("class"), model ) ); } catch ( ClassNotFoundException cnfe ) { throw new MappingException("class not found for alias: " + alias, cnfe); } namedQuery.addAliasedClass(alias, clazz); } Iterator tables = queryElem.elementIterator("synchronize"); while ( tables.hasNext() ) { namedQuery.addSynchronizedTable( ( (Element) tables.next() ).attributeValue("table") ); } log.debug( "Named sql query: " + queryName + " -> " + namedQuery.getQueryString() ); model.addSQLQuery(queryName, namedQuery); } nodes = hmNode.elementIterator("import"); while ( nodes.hasNext() ) { Element n = (Element) nodes.next(); String className = getClassName( n.attribute("class"), model ); Attribute renameNode = n.attribute("rename"); String rename = (renameNode==null) ? StringHelper.unqualify(className) : renameNode.getValue(); log.debug("Import: " + rename + " -> " + className); model.addImport(className, rename); } } private static PersistentClass getSuperclass(Mappings model, Element subnode) throws MappingException { String extendsValue = subnode.attributeValue("extends"); Class superclass; try { superclass = ReflectHelper.classForName(extendsValue); } catch (ClassNotFoundException e) { throw new MappingException("extends class " + extendsValue + " not found.", e); } PersistentClass superModel = model.getClass(superclass); if (superModel==null) { throw new MappingException( "Cannot extend unmapped class " + superclass.getName() ); } return superModel; } abstract static class SecondPass { Element node; Mappings mappings; Collection collection; SecondPass(Element node, Mappings mappings, Collection collection) { this.node = node; this.collection = collection; this.mappings = mappings; } final void doSecondPass(java.util.Map persistentClasses) throws MappingException { if ( log.isDebugEnabled() ) log.debug("Second pass for collection: " + collection.getRole() ); secondPass(persistentClasses); collection.createAllKeys(); if ( log.isDebugEnabled() ) { String msg = "Mapped collection key: " + columns( collection.getKey() ); if ( collection.isIndexed() ) msg+= ", index: " + columns( ( (IndexedCollection) collection ).getIndex() ); if ( collection.isOneToMany() ) { msg += ", one-to-many: " + collection.getElement().getType().getName(); } else { msg += ", element: " + columns( collection.getElement() ); msg += ", type: " + collection.getElement().getType().getName(); } log.debug(msg); } } abstract void secondPass(java.util.Map persistentClasses) throws MappingException; } static class CollectionSecondPass extends SecondPass { CollectionSecondPass(Element node, Mappings mappings, Collection collection) { super(node, mappings, collection); } void secondPass(java.util.Map persistentClasses) throws MappingException { Binder.bindCollectionSecondPass( node, collection, persistentClasses, mappings ); } } static class IdentifierCollectionSecondPass extends SecondPass { IdentifierCollectionSecondPass(Element node, Mappings mappings, IdentifierCollection collection) { super(node, mappings, collection); } void secondPass(java.util.Map persistentClasses) throws MappingException { Binder.bindIdentifierCollectionSecondPass( node, (IdentifierCollection) collection, persistentClasses, mappings ); } } static class MapSecondPass extends SecondPass { MapSecondPass(Element node, Mappings mappings, Map collection) { super(node, mappings, collection); } void secondPass(java.util.Map persistentClasses) throws MappingException { Binder.bindMapSecondPass( node, (Map) collection, persistentClasses, mappings ); } } static class ListSecondPass extends SecondPass { ListSecondPass(Element node, Mappings mappings, List collection) { super(node, mappings, collection); } void secondPass(java.util.Map persistentClasses) throws MappingException { Binder.bindListSecondPass( node, (List) collection, persistentClasses, mappings ); } } //This inner class implements a case statement....perhaps im being a bit over-clever here abstract static class CollectionType { private String xmlTag; public abstract Collection create(Element node, String className, String path, PersistentClass owner, Mappings mappings) throws MappingException; CollectionType(String xmlTag) { this.xmlTag = xmlTag; } public String toString() { return xmlTag; } private static final CollectionType MAP = new CollectionType("map") { public Collection create(Element node, String prefix, String path, PersistentClass owner, Mappings mappings) throws MappingException { Map map = new Map(owner); bindCollection(node, map, prefix, path, mappings); return map; } }; private static final CollectionType SET = new CollectionType("set") { public Collection create(Element node, String prefix, String path, PersistentClass owner, Mappings mappings) throws MappingException { Set set = new Set(owner); bindCollection(node, set, prefix, path, mappings); return set; } }; private static final CollectionType LIST = new CollectionType("list") { public Collection create(Element node, String prefix, String path, PersistentClass owner, Mappings mappings) throws MappingException { List list = new List(owner); bindCollection(node, list, prefix, path, mappings); return list; } }; private static final CollectionType BAG = new CollectionType("bag") { public Collection create(Element node, String prefix, String path, PersistentClass owner, Mappings mappings) throws MappingException { Bag bag = new Bag(owner); bindCollection(node, bag, prefix, path, mappings); return bag; } }; private static final CollectionType IDBAG = new CollectionType("idbag") { public Collection create(Element node, String prefix, String path, PersistentClass owner, Mappings mappings) throws MappingException { IdentifierBag bag = new IdentifierBag(owner); bindCollection(node, bag, prefix, path, mappings); return bag; } }; private static final CollectionType ARRAY = new CollectionType("array") { public Collection create(Element node, String prefix, String path, PersistentClass owner, Mappings mappings) throws MappingException { Array array = new Array(owner); bindArray(node, array, prefix, path, mappings); return array; } }; private static final CollectionType PRIMITIVE_ARRAY = new CollectionType("primitive-array") { public Collection create(Element node, String prefix, String path, PersistentClass owner, Mappings mappings) throws MappingException { PrimitiveArray array = new PrimitiveArray(owner); bindArray(node, array, prefix, path, mappings); return array; } }; private static final HashMap INSTANCES = new HashMap(); static { INSTANCES.put(MAP.toString(), MAP); INSTANCES.put(BAG.toString(), BAG); INSTANCES.put(IDBAG.toString(), IDBAG); INSTANCES.put(SET.toString(), SET); INSTANCES.put(LIST.toString(), LIST); INSTANCES.put(ARRAY.toString(), ARRAY); INSTANCES.put(PRIMITIVE_ARRAY.toString(), PRIMITIVE_ARRAY); } public static CollectionType collectionTypeFromString(String xmlTagName) { return (CollectionType) INSTANCES.get(xmlTagName); } } private static int getOptimisticLockMode(Attribute olAtt) throws MappingException { if (olAtt==null) return Versioning.OPTIMISTIC_LOCK_VERSION; String olMode = olAtt.getValue(); if ( olMode==null || "version".equals(olMode) ) { return Versioning.OPTIMISTIC_LOCK_VERSION; } else if ( "dirty".equals(olMode) ) { return Versioning.OPTIMISTIC_LOCK_DIRTY; } else if ( "all".equals(olMode) ) { return Versioning.OPTIMISTIC_LOCK_ALL; } else if ( "none".equals(olMode) ) { return Versioning.OPTIMISTIC_LOCK_NONE; } else { throw new MappingException("Unsupported optimistic-lock style: " + olMode); } } private static final java.util.Map getMetas(Element node) { java.util.Map map = new HashMap(); Iterator iter = node.elementIterator("meta"); while ( iter.hasNext() ) { Element metaNode = (Element) iter.next(); String name = metaNode.attributeValue("attribute"); MetaAttribute meta = (MetaAttribute) map.get(name); if (meta==null) { meta = new MetaAttribute(); map.put(name, meta); } meta.addValue( metaNode.getText() ); } return map; } private static String getClassName(Attribute att, Mappings model) { String result = att.getValue(); if (result==null) return null; if ( result.indexOf('.')<0 && model.getDefaultPackage()!=null ) { result = model.getDefaultPackage() + StringHelper.DOT + result; } return result; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -