From: <one...@us...> - 2003-01-27 12:12:43
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg In directory sc8-pr-cvs1:/tmp/cvs-serv24603/sf/hibernate/cfg Modified Files: Binder.java Configuration.java Added Files: Mappings.java Log Message: further clean-ups of mapping package --- NEW FILE: Mappings.java --- //$Id: Mappings.java,v 1.1 2003/01/27 12:12:40 oneovthafew Exp $ package net.sf.hibernate.cfg; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import net.sf.hibernate.MappingException; import net.sf.hibernate.cache.Cache; import net.sf.hibernate.cache.CacheConcurrencyStrategy; import net.sf.hibernate.cache.CacheException; import net.sf.hibernate.cache.JCSCache; import net.sf.hibernate.cache.ReadOnlyCache; import net.sf.hibernate.cache.ReadWriteCache; import net.sf.hibernate.mapping.Collection; import net.sf.hibernate.mapping.PersistentClass; import net.sf.hibernate.mapping.Table; import net.sf.hibernate.util.StringHelper; /** * A collection of mappings from classes and collections to * relational database tables. (Represents a single * <tt><hibernate-mapping><tt> element.) */ public class Mappings { public static final String ROOT_ROLE_NAME = StringHelper.EMPTY_STRING; public static final char ROLE_SEPERATOR = '/'; private static final Log log = LogFactory.getLog(Mappings.class); private final Map classes; private final Map collections; private final Map tables; private final Map queries; private final List secondPasses; private String schemaName; private String defaultCascade; Mappings(Map classes, Map collections, Map tables, Map queries, List secondPasses) { this.classes = classes; this.collections = collections; this.queries = queries; this.tables = tables; this.secondPasses = secondPasses; } public void addClass(PersistentClass persistentClass) throws MappingException { Object old = classes.put( persistentClass.getPersistentClass(), persistentClass ); if ( old!=null ) throw new MappingException( "duplicate class mapping: " + persistentClass.getPersistentClass().getName() ); } public void addCollection(Collection collection) throws MappingException { Object old = collections.put( collection.getRole(), collection ); if ( old!=null ) throw new MappingException( "duplicate collection role: " + collection.getRole() ); } public PersistentClass getClass(Class clazz) { return (PersistentClass) classes.get(clazz); } public Collection getCollection(String role) { return (Collection) collections.get(role); } public Table addTable(String schema, String name) { String key = schema != null ? schema + "." + name : name; Table table = (Table) tables.get(key); if (table == null) { table = new Table(); table.setName(name); table.setSchema(schema); tables.put(key, table); } return table; } public Table getTable(String schema, String name) { String key = schema != null ? schema + "." + name : name; return (Table) tables.get(key); } public String getSchemaName() { return schemaName; } public String getDefaultCascade() { return defaultCascade; } public CacheConcurrencyStrategy createJCSCache(String usage, String name, PersistentClass owner) throws MappingException { final Cache jcs; try { jcs = new JCSCache(); } catch (NoClassDefFoundError ncf) { log.warn( "Could not instantiate cache - probably the JCS jar is missing", ncf ); // continue with no cache return null; } try { jcs.setClass(name); } catch (CacheException ce) { throw new MappingException("Could not instantiate JCS",ce); } if ( usage.equals("read-only") ) { if ( owner.isMutable() ) log.warn( "read-only cache configured for mutable: " + name ); return new ReadOnlyCache(jcs); } else if ( usage.equals("read-write") ) { return new ReadWriteCache(jcs); } else { throw new MappingException("jcs-cache usage attribute should be read-write or read-only"); } } /** * Sets the schemaName. * @param schemaName The schemaName to set */ public void setSchemaName(String schemaName) { this.schemaName = schemaName; } /** * Sets the defaultCascade. * @param defaultCascade The defaultCascade to set */ public void setDefaultCascade(String defaultCascade) { this.defaultCascade = defaultCascade; } public void addQuery(String name, String query) throws MappingException { Object old = queries.put(name, query); if (old!=null) throw new MappingException("duplicate query name: " + name); } public String getQuery(String name) { return (String) queries.get(name); } void addSecondPass(Binder.SecondPass sp) { secondPasses.add(sp); } } Index: Binder.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg/Binder.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Binder.java 27 Jan 2003 07:12:03 -0000 1.1 --- Binder.java 27 Jan 2003 12:12:40 -0000 1.2 *************** *** 26,33 **** import net.sf.hibernate.mapping.OneToOne; import net.sf.hibernate.mapping.PersistentClass; - import net.sf.hibernate.mapping.PrimaryKey; import net.sf.hibernate.mapping.PrimitiveArray; import net.sf.hibernate.mapping.Property; - import net.sf.hibernate.mapping.Root; import net.sf.hibernate.mapping.RootClass; import net.sf.hibernate.mapping.Set; --- 26,31 ---- *************** *** 57,61 **** import org.w3c.dom.NodeList; ! public class Binder { private static final Log log = LogFactory.getLog(Collection.class); --- 55,59 ---- import org.w3c.dom.NodeList; ! class Binder { private static final Log log = LogFactory.getLog(Collection.class); *************** *** 96,167 **** } ! public static void bindSubclass(Node node, Subclass model, Root root) throws MappingException { bindClass(node, model); ! NamedNodeMap atts = node.getAttributes(); ! ! String mappingStyle = node.getNodeName(); ! // TODO: split this class into 2 different subclasses ! // one for joined, one for "normal" (same table) ! if ( "subclass".equals(mappingStyle) ) { ! //ordinary subclasses ! ! if ( model.getPersister()==null ) { ! model.getRootClass().setPersister(EntityPersister.class); ! } ! ! model.setTable( model.getSuperclass().getTable() ); ! } ! else { ! ! Node tableNameNode = atts.getNamedItem("table"); ! ! if ( model.getPersister()==null ) { ! model.getRootClass().setPersister(NormalizedEntityPersister.class); ! } ! ! // joined subclasses ! String tableName = (tableNameNode==null) ? ! StringHelper.unqualify( model.getPersistentClass().getName() ) : ! tableNameNode.getNodeValue(); ! Node schemaNode = atts.getNamedItem("schema"); ! String schema = schemaNode==null ? root.getSchemaName() : schemaNode.getNodeValue(); ! Table mytable = root.addTable(schema, tableName); ! model.setTable(mytable); ! ! //Primary key constraint ! PrimaryKey pk = new PrimaryKey(); ! pk.setTable(mytable); ! pk.setName( StringHelper.suffix( tableName, "PK" ) ); ! mytable.setPrimaryKey(pk); ! ! NodeList subnodes = node.getChildNodes(); ! for ( int i=0; i<subnodes.getLength(); i++ ) { ! if ( "key".equals( subnodes.item(i).getNodeName() ) ) { ! Value key = new Value(mytable); ! model.setKey(key); ! bindValue( subnodes.item(i), key, false, Root.ROOT_ROLE_NAME ); ! } ! } ! ! model.getKey().setType( model.getIdentifier().getType() ); ! ! Iterator iter = model.getKey().getColumnIterator(); ! while ( iter.hasNext() ) { ! Column col = (Column) iter.next(); ! pk.addColumn(col); ! } ! ! ForeignKey fk = mytable.createForeignKey( model.getKey().getConstraintColumns() ); ! fk.setReferencedClass( model.getSuperclass().getPersistentClass() ); ! } // properties ! propertiesFromXML(node, model, root); } ! public static void bindRootClass(Node node, RootClass model, Root root) throws MappingException { bindClass(node, model); --- 94,152 ---- } ! public static void bindSubclass(Node node, Subclass model, Mappings mappings) throws MappingException { bindClass(node, model); ! if ( model.getPersister()==null ) { ! model.getRootClass().setPersister(EntityPersister.class); } + model.setTable( model.getSuperclass().getTable() ); + // properties ! propertiesFromXML(node, model, mappings); } ! public static void bindJoinedSubclass(Node node, Subclass model, Mappings mappings) throws MappingException { ! ! bindClass(node, model); ! ! NamedNodeMap atts = node.getAttributes(); ! ! Node tableNameNode = atts.getNamedItem("table"); ! ! if ( model.getPersister()==null ) { ! model.getRootClass().setPersister(NormalizedEntityPersister.class); ! } ! ! // joined subclasses ! String tableName = (tableNameNode==null) ? ! StringHelper.unqualify( model.getPersistentClass().getName() ) : ! tableNameNode.getNodeValue(); ! Node schemaNode = atts.getNamedItem("schema"); ! String schema = schemaNode==null ? mappings.getSchemaName() : schemaNode.getNodeValue(); ! Table mytable = mappings.addTable(schema, tableName); ! model.setTable(mytable); ! ! NodeList subnodes = node.getChildNodes(); ! for ( int i=0; i<subnodes.getLength(); i++ ) { ! if ( "key".equals( subnodes.item(i).getNodeName() ) ) { ! Value key = new Value(mytable); ! model.setKey(key); ! bindValue( subnodes.item(i), key, false, Mappings.ROOT_ROLE_NAME ); ! } ! } ! ! model.getKey().setType( model.getIdentifier().getType() ); ! model.createPrimaryKey(); ! ForeignKey fk = mytable.createForeignKey( model.getKey().getConstraintColumns() ); ! fk.setReferencedClass( model.getSuperclass().getPersistentClass() ); ! ! ! // properties ! propertiesFromXML(node, model, mappings); ! } ! ! public static void bindRootClass(Node node, RootClass model, Mappings mappings) throws MappingException { bindClass(node, model); *************** *** 176,181 **** Node schemaNode = atts.getNamedItem("schema"); ! String schema = schemaNode==null ? root.getSchemaName() : schemaNode.getNodeValue(); ! Table table = root.addTable(schema, tableName); model.setTable(table); --- 161,166 ---- Node schemaNode = atts.getNamedItem("schema"); ! String schema = schemaNode==null ? mappings.getSchemaName() : schemaNode.getNodeValue(); ! Table table = mappings.addTable(schema, tableName); model.setTable(table); *************** *** 221,225 **** model.getIdentifier().setTypeByReflection( model.getPersistentClass(), propertyName ); Property prop = new Property(); ! bindProperty(subnode, prop, model.getIdentifier(), root); model.setIdentifierProperty(prop); } --- 206,210 ---- model.getIdentifier().setTypeByReflection( model.getPersistentClass(), propertyName ); Property prop = new Property(); ! bindProperty(subnode, prop, model.getIdentifier(), mappings); model.setIdentifierProperty(prop); } *************** *** 232,236 **** if (propertyName==null) { model.setIdentifier( new Component(model) ); ! bindComponent(subnode, (Component) model.getIdentifier(), null, null, false, root); model.setEmbeddedIdentifier( ( (Component) model.getIdentifier() ).isEmbedded() ); model.setIdentifierProperty(null); --- 217,221 ---- if (propertyName==null) { model.setIdentifier( new Component(model) ); ! bindComponent(subnode, (Component) model.getIdentifier(), null, null, false, mappings); model.setEmbeddedIdentifier( ( (Component) model.getIdentifier() ).isEmbedded() ); model.setIdentifierProperty(null); *************** *** 239,245 **** Class reflectedClass = ReflectHelper.getGetter( model.getPersistentClass(), propertyName ).getReturnType(); model.setIdentifier( new Component(model) ); ! bindComponent(subnode, (Component) model.getIdentifier(), reflectedClass, null, false, root); Property prop = new Property(); ! bindProperty(subnode, prop, model.getIdentifier(), root); model.setIdentifierProperty(prop); } --- 224,230 ---- Class reflectedClass = ReflectHelper.getGetter( model.getPersistentClass(), propertyName ).getReturnType(); model.setIdentifier( new Component(model) ); ! bindComponent(subnode, (Component) model.getIdentifier(), reflectedClass, null, false, mappings); Property prop = new Property(); ! bindProperty(subnode, prop, model.getIdentifier(), mappings); model.setIdentifierProperty(prop); } *************** *** 252,256 **** if ( val.getType()==null ) val.setType( "version".equals(name) ? Hibernate.INTEGER : Hibernate.TIMESTAMP ); Property prop = new Property(); ! bindProperty(subnode, prop, val, root); model.setVersion(prop); model.addProperty(prop); --- 237,241 ---- if ( val.getType()==null ) val.setType( "version".equals(name) ? Hibernate.INTEGER : Hibernate.TIMESTAMP ); Property prop = new Property(); ! bindProperty(subnode, prop, val, mappings); model.setVersion(prop); model.addProperty(prop); *************** *** 267,271 **** } else if ( "jcs-cache".equals(name) ) { ! model.setCache( root.createJCSCache( subnode.getAttributes().getNamedItem("usage").getNodeValue(), model.getPersistentClass().getName(), --- 252,256 ---- } else if ( "jcs-cache".equals(name) ) { ! model.setCache( mappings.createJCSCache( subnode.getAttributes().getNamedItem("usage").getNodeValue(), model.getPersistentClass().getName(), *************** *** 274,290 **** } ! } //Primary key constraint ! PrimaryKey pk = new PrimaryKey(); ! pk.setTable(table); ! pk.setName( StringHelper.suffix( table.getName(), "PK" ) ); ! Iterator iter = model.getIdentifier().getColumnIterator(); ! while ( iter.hasNext() ) { ! pk.addColumn( (Column) iter.next() ); ! } ! //root.addPrimaryKey(pk); //No good for postgres ! table.setPrimaryKey(pk); ! propertiesFromXML(node, model, root); } --- 259,268 ---- } ! } ! //Primary key constraint ! model.createPrimaryKey(); ! propertiesFromXML(node, model, mappings); } *************** *** 354,358 **** } ! public static void bindProperty(Node node, Property model, Value value, Root root) throws MappingException { model.setName( Property.getPropertyName(node) ); model.setValue(value); --- 332,336 ---- } ! public static void bindProperty(Node node, Property model, Value value, Mappings mappings) throws MappingException { model.setName( Property.getPropertyName(node) ); model.setValue(value); *************** *** 361,365 **** Node cascadeNode = node.getAttributes().getNamedItem("cascade"); model.setCascade( (cascadeNode==null) ? ! root.getDefaultCascade() : cascadeNode.getNodeValue() ); --- 339,343 ---- Node cascadeNode = node.getAttributes().getNamedItem("cascade"); model.setCascade( (cascadeNode==null) ? ! mappings.getDefaultCascade() : cascadeNode.getNodeValue() ); *************** *** 370,377 **** ); } - public static void bindCollection(Node node, Collection model, String prefix, Root root) throws MappingException { - model.node = node; - model.root = root; NamedNodeMap atts = node.getAttributes(); //ROLENAME --- 348,357 ---- ); } + + /** + * Called for all collections + */ + public static void bindCollection(Node node, Collection model, String prefix, Mappings mappings) throws MappingException { NamedNodeMap atts = node.getAttributes(); //ROLENAME *************** *** 379,383 **** if (roleAtt==null) roleAtt = atts.getNamedItem("role"); String barerole = roleAtt.getNodeValue(); ! model.setRole( prefix + Root.ROLE_SEPERATOR + barerole ); Node inverseNode = atts.getNamedItem("inverse"); --- 359,363 ---- if (roleAtt==null) roleAtt = atts.getNamedItem("role"); String barerole = roleAtt.getNodeValue(); ! model.setRole( prefix + Mappings.ROLE_SEPERATOR + barerole ); Node inverseNode = atts.getNamedItem("inverse"); *************** *** 418,423 **** } Node schemaNode = atts.getNamedItem("schema"); ! String schema = schemaNode==null ? root.getSchemaName() : schemaNode.getNodeValue(); ! model.setTable( root.addTable(schema, tableName) ); } //LAZINESS --- 398,403 ---- } Node schemaNode = atts.getNamedItem("schema"); ! String schema = schemaNode==null ? mappings.getSchemaName() : schemaNode.getNodeValue(); ! model.setTable( mappings.addTable(schema, tableName) ); } //LAZINESS *************** *** 444,447 **** --- 424,441 ---- } } + } + + //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 { + mappings.addSecondPass( new CollectionSecondPass(node, mappings, model) ); } } *************** *** 522,529 **** model.setSqlType( (typeNode==null) ? null : typeNode.getNodeValue() ); } ! ! public static void bindArray(Node node, Array model, String prefix, Root root) throws MappingException { - bindCollection(node, model, prefix, root); Node att = node.getAttributes().getNamedItem("element-class"); --- 516,527 ---- model.setSqlType( (typeNode==null) ? null : typeNode.getNodeValue() ); } ! ! /** ! * Called for arrays and primitive arrays ! */ ! public static void bindArray(Node node, Array model, String prefix, Mappings mappings) throws MappingException { ! ! bindCollection(node, model, prefix, mappings); Node att = node.getAttributes().getNamedItem("element-class"); *************** *** 566,570 **** } ! public static void bindComponent(Node node, Component model, Class reflectedClass, String path, boolean isNullable, Root root) throws MappingException { Node classNode = node.getAttributes().getNamedItem("class"); --- 564,568 ---- } ! public static void bindComponent(Node node, Component model, Class reflectedClass, String path, boolean isNullable, Mappings mappings) throws MappingException { Node classNode = node.getAttributes().getNamedItem("class"); *************** *** 600,604 **** //component path ! path += Root.ROLE_SEPERATOR + StringHelper.unqualify(className); NodeList list = node.getChildNodes(); --- 598,602 ---- //component path ! path += Mappings.ROLE_SEPERATOR + StringHelper.unqualify(className); NodeList list = node.getChildNodes(); *************** *** 612,617 **** Value value = null; if (collectType!=null) { ! Collection collection = collectType.create( list.item(i), path, model.getOwner(), root ); ! root.addCollection(collection); value = new Value( model.getTable() ); bindValue(subnode, value, isNullable); --- 610,615 ---- Value value = null; if (collectType!=null) { ! Collection collection = collectType.create( list.item(i), path, model.getOwner(), mappings ); ! mappings.addCollection(collection); value = new Value( model.getTable() ); bindValue(subnode, value, isNullable); *************** *** 637,641 **** new Component( model.getOwner() ) : // a class component new Component( model.getTable() ); // a composite element ! bindComponent(subnode, (Component) value, subreflectedClass, path, isNullable, root); } else if ( "parent".equals(name) ) { --- 635,639 ---- new Component( model.getOwner() ) : // a class component new Component( model.getTable() ); // a composite element ! bindComponent(subnode, (Component) value, subreflectedClass, path, isNullable, mappings); } else if ( "parent".equals(name) ) { *************** *** 647,651 **** value.createForeignKey(); Property prop = new Property(); ! bindProperty(subnode, prop, value, root); model.addProperty(prop); } --- 645,649 ---- value.createForeignKey(); Property prop = new Property(); ! bindProperty(subnode, prop, value, mappings); model.addProperty(prop); } *************** *** 746,750 **** } ! protected static void propertiesFromXML(Node node, PersistentClass model, Root root) throws MappingException { NodeList list = node.getChildNodes(); --- 744,748 ---- } ! protected static void propertiesFromXML(Node node, PersistentClass model, Mappings mappings) throws MappingException { NodeList list = node.getChildNodes(); *************** *** 760,765 **** Value value = null; if (collectType!=null) { ! Collection collection = collectType.create(subnode, path, model, root); ! root.addCollection(collection); value = new Value(table); bindValue(subnode, value, true); --- 758,763 ---- Value value = null; if (collectType!=null) { ! Collection collection = collectType.create(subnode, path, model, mappings); ! mappings.addCollection(collection); value = new Value(table); bindValue(subnode, value, true); *************** *** 781,797 **** Class reflectedClass = ReflectHelper.getGetter( model.getPersistentClass(), propertyName ).getReturnType(); value = new Component(model); ! bindComponent(subnode, (Component) value, reflectedClass, path, true, root); } ! else if ( "subclass".equals(name) || "joined-subclass".equals(name) ) { Subclass subclass = new Subclass(model); ! bindSubclass( list.item(i), subclass, root ); model.addSubclass(subclass); ! root.addPersistentClass(subclass); } if ( value!=null) { value.setTypeByReflection( model.getPersistentClass(), propertyName ); value.createForeignKey(); Property prop = new Property(); ! bindProperty(subnode, prop, value, root); model.addProperty(prop); } --- 779,801 ---- Class reflectedClass = ReflectHelper.getGetter( model.getPersistentClass(), propertyName ).getReturnType(); value = new Component(model); ! bindComponent(subnode, (Component) value, reflectedClass, path, true, mappings); } ! else if ( "subclass".equals(name) ) { Subclass subclass = new Subclass(model); ! bindSubclass( list.item(i), subclass, mappings ); model.addSubclass(subclass); ! mappings.addClass(subclass); } + else if ( "joined-subclass".equals(name) ) { + Subclass subclass = new Subclass(model); + bindJoinedSubclass( list.item(i), subclass, mappings ); + model.addSubclass(subclass); + mappings.addClass(subclass); + } if ( value!=null) { value.setTypeByReflection( model.getPersistentClass(), propertyName ); value.createForeignKey(); Property prop = new Property(); ! bindProperty(subnode, prop, value, mappings); model.addProperty(prop); } *************** *** 799,819 **** } ! public static void bindSetSecondPass(Set model, java.util.Map persistentClasses) throws MappingException { ! if (model.doneSecondPass) return; ! ! bindCollectionSecondPass(model, persistentClasses); if ( !model.isOneToMany() ) model.createPrimaryKey(); } ! ! public static void bindListSecondPass(List model, java.util.Map classes) throws MappingException { ! ! if (model.doneSecondPass) return; ! bindCollectionSecondPass(model, classes); ! NodeList list = model.node.getChildNodes(); for ( int i=0; i<list.getLength(); i++ ) { Node subnode = list.item(i); --- 803,822 ---- } ! public static void bindSetSecondPass(Node node, Set model, java.util.Map persistentClasses, Mappings mappings) throws MappingException { ! bindCollectionSecondPass(node, model, persistentClasses, mappings); if ( !model.isOneToMany() ) model.createPrimaryKey(); } ! ! /** ! * Called for Lists, arrays, primitive arrays ! */ ! public static void bindListSecondPass(Node node, List model, java.util.Map classes, Mappings mappings) throws MappingException { ! bindCollectionSecondPass(node, model, classes, mappings); ! NodeList list = node.getChildNodes(); for ( int i=0; i<list.getLength(); i++ ) { Node subnode = list.item(i); *************** *** 828,841 **** if ( !model.isOneToMany() ) model.createPrimaryKey(); - model.doneSecondPass = true; } ! public static void bindMapSecondPass(Map model, java.util.Map classes) throws MappingException { ! ! if (model.doneSecondPass) return; ! bindCollectionSecondPass(model, classes); ! NodeList list = model.node.getChildNodes(); for ( int i=0; i<list.getLength(); i++ ) { Node subnode = list.item(i); --- 831,844 ---- if ( !model.isOneToMany() ) model.createPrimaryKey(); } ! /** ! * Called for Maps ! */ ! public static void bindMapSecondPass(Node node, Map model, java.util.Map classes, Mappings mappings) throws MappingException { ! bindCollectionSecondPass(node, model, classes, mappings); ! NodeList list = node.getChildNodes(); for ( int i=0; i<list.getLength(); i++ ) { Node subnode = list.item(i); *************** *** 856,860 **** else if ( "composite-index".equals(name) ) { Component component = new Component( model.getTable() ); ! bindComponent(subnode, component, null, Root.ROOT_ROLE_NAME, model.isOneToMany(), model.root); model.setIndex(component); } --- 859,863 ---- else if ( "composite-index".equals(name) ) { Component component = new Component( model.getTable() ); ! bindComponent(subnode, component, null, Mappings.ROOT_ROLE_NAME, model.isOneToMany(), mappings); model.setIndex(component); } *************** *** 864,874 **** if ( !model.isOneToMany() ) model.createPrimaryKey(); - model.doneSecondPass = true; } ! ! public static void bindCollectionSecondPass(Collection model, java.util.Map persistentClasses) throws MappingException { - if (model.doneSecondPass) return; - if ( model.isOneToMany() ) { Class assocClass = model.getOneToMany().getType().getPersistentClass(); --- 867,877 ---- if ( !model.isOneToMany() ) model.createPrimaryKey(); } ! ! /** ! * Called for all collections ! */ ! public static void bindCollectionSecondPass(Node node, Collection model, java.util.Map persistentClasses, Mappings mappings) throws MappingException { if ( model.isOneToMany() ) { Class assocClass = model.getOneToMany().getType().getPersistentClass(); *************** *** 880,884 **** } ! NodeList list = model.node.getChildNodes(); for ( int i=0; i<list.getLength(); i++ ) { Node subnode = list.item(i); --- 883,887 ---- } ! NodeList list = node.getChildNodes(); for ( int i=0; i<list.getLength(); i++ ) { Node subnode = list.item(i); *************** *** 907,914 **** Component element = new Component( model.getTable() ); model.setElement(element); ! bindComponent(subnode, element, null, Root.ROOT_ROLE_NAME, true, model.root); } else if ( "jcs-cache".equals(name) ) { ! model.setCache( model.root.createJCSCache( subnode.getAttributes().getNamedItem("usage").getNodeValue(), model.getRole(), --- 910,917 ---- Component element = new Component( model.getTable() ); model.setElement(element); ! bindComponent(subnode, element, null, Mappings.ROOT_ROLE_NAME, true, mappings); } else if ( "jcs-cache".equals(name) ) { ! model.setCache( mappings.createJCSCache( subnode.getAttributes().getNamedItem("usage").getNodeValue(), model.getRole(), *************** *** 927,934 **** if ( !model.isIndexed() ) model.createIndex(); - model.doneSecondPass=true; } ! public static void bindRoot(Document doc, Root model) throws MappingException { Node hmNode = doc.getElementsByTagName("hibernate-mapping").item(0); Node schemaNode = hmNode.getAttributes().getNamedItem("schema"); --- 930,937 ---- if ( !model.isIndexed() ) model.createIndex(); } ! public static void bindRoot(Document doc, Mappings model) throws MappingException { ! Node hmNode = doc.getElementsByTagName("hibernate-mapping").item(0); Node schemaNode = hmNode.getAttributes().getNamedItem("schema"); *************** *** 945,950 **** RootClass rootclass = new RootClass(); Binder.bindRootClass(n, rootclass, model); ! log.debug("Root class: " + rootclass.getName() + " -> " + rootclass.getTable().getName() ); ! model.addPersistentClass(rootclass); } else if ( name.equals("query") ) { --- 948,953 ---- RootClass rootclass = new RootClass(); Binder.bindRootClass(n, rootclass, model); ! log.debug("Mapping class: " + rootclass.getName() + " -> " + rootclass.getTable().getName() ); ! model.addClass(rootclass); } else if ( name.equals("query") ) { *************** *** 957,967 **** } } //This inner class implements a case statement....perhaps im being a bit over-clever here ! public static abstract class CollectionType { private String xmlTag; ! public abstract Collection create(Node node, String prefix, PersistentClass owner, Root root) throws MappingException; CollectionType(String xmlTag) { this.xmlTag = xmlTag; --- 960,1021 ---- } + } + + static abstract class SecondPass { + Node node; + Mappings mappings; + Collection collection; + SecondPass(Node node, Mappings mappings, Collection collection) { + this.node = node; + this.collection = collection; + this.mappings = mappings; + } + abstract void secondPass(java.util.Map persistentClasses) throws MappingException; + } + + static class CollectionSecondPass extends SecondPass { + CollectionSecondPass(Node 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 MapSecondPass extends SecondPass { + MapSecondPass(Node 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 SetSecondPass extends SecondPass { + SetSecondPass(Node node, Mappings mappings, Set collection) { + super(node, mappings, collection); + } + void secondPass(java.util.Map persistentClasses) throws MappingException { + Binder.bindSetSecondPass( node, (Set) collection, persistentClasses, mappings ); + } + + } + + static class ListSecondPass extends SecondPass { + ListSecondPass(Node 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 ! static abstract class CollectionType { private String xmlTag; ! public abstract Collection create(Node node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException; CollectionType(String xmlTag) { this.xmlTag = xmlTag; *************** *** 971,1012 **** } private static final CollectionType MAP = new CollectionType("map") { ! public Collection create(Node node, String prefix, PersistentClass owner, Root root) throws MappingException { Map map = new Map(owner); ! Binder.bindCollection(node, map, prefix, root); return map; } }; private static final CollectionType SET = new CollectionType("set") { ! public Collection create(Node node, String prefix, PersistentClass owner, Root root) throws MappingException { Set set = new Set(owner); ! Binder.bindCollection(node, set, prefix, root); return set; } }; private static final CollectionType LIST = new CollectionType("list") { ! public Collection create(Node node, String prefix, PersistentClass owner, Root root) throws MappingException { List list = new List(owner); ! Binder.bindCollection(node, list, prefix, root); return list; } }; private static final CollectionType BAG = new CollectionType("bag") { ! public Collection create(Node node, String prefix, PersistentClass owner, Root root) throws MappingException { Bag bag = new Bag(owner); ! Binder.bindCollection(node, bag, prefix, root); return bag; } }; private static final CollectionType ARRAY = new CollectionType("array") { ! public Collection create(Node node, String prefix, PersistentClass owner, Root root) throws MappingException { Array array = new Array(owner); ! Binder.bindArray(node, array, prefix, root); return array; } }; private static final CollectionType PRIMITIVE_ARRAY = new CollectionType("primitive-array") { ! public Collection create(Node node, String prefix, PersistentClass owner, Root root) throws MappingException { PrimitiveArray array = new PrimitiveArray(owner); ! Binder.bindArray(node, array, prefix, root); return array; } --- 1025,1066 ---- } private static final CollectionType MAP = new CollectionType("map") { ! public Collection create(Node node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException { Map map = new Map(owner); ! Binder.bindCollection(node, map, prefix, mappings); return map; } }; private static final CollectionType SET = new CollectionType("set") { ! public Collection create(Node node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException { Set set = new Set(owner); ! Binder.bindCollection(node, set, prefix, mappings); return set; } }; private static final CollectionType LIST = new CollectionType("list") { ! public Collection create(Node node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException { List list = new List(owner); ! Binder.bindCollection(node, list, prefix, mappings); return list; } }; private static final CollectionType BAG = new CollectionType("bag") { ! public Collection create(Node node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException { Bag bag = new Bag(owner); ! Binder.bindCollection(node, bag, prefix, mappings); return bag; } }; private static final CollectionType ARRAY = new CollectionType("array") { ! public Collection create(Node node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException { Array array = new Array(owner); ! Binder.bindArray(node, array, prefix, mappings); return array; } }; private static final CollectionType PRIMITIVE_ARRAY = new CollectionType("primitive-array") { ! public Collection create(Node node, String prefix, PersistentClass owner, Mappings mappings) throws MappingException { PrimitiveArray array = new PrimitiveArray(owner); ! Binder.bindArray(node, array, prefix, mappings); return array; } Index: Configuration.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg/Configuration.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** Configuration.java 27 Jan 2003 07:12:03 -0000 1.7 --- Configuration.java 27 Jan 2003 12:12:40 -0000 1.8 *************** *** 34,47 **** import net.sf.hibernate.id.PersistentIdentifierGenerator; import net.sf.hibernate.impl.SessionFactoryImpl; - import net.sf.hibernate.mapping.Array; - import net.sf.hibernate.mapping.Bag; import net.sf.hibernate.mapping.ForeignKey; import net.sf.hibernate.mapping.Index; - import net.sf.hibernate.mapping.List; - import net.sf.hibernate.mapping.Map; import net.sf.hibernate.mapping.PersistentClass; - import net.sf.hibernate.mapping.PrimitiveArray; - import net.sf.hibernate.mapping.Root; - import net.sf.hibernate.mapping.Set; import net.sf.hibernate.mapping.Table; import net.sf.hibernate.dialect.Dialect; --- 34,40 ---- *************** *** 70,73 **** --- 63,67 ---- private HashMap tables = new HashMap(); private HashMap namedQueries = new HashMap(); + private ArrayList secondPasses = new ArrayList(); private Interceptor interceptor = EMPTY_INTERCEPTOR; private Properties properties = Environment.getProperties(); *************** *** 160,164 **** private void add(Document doc) throws Exception { try { ! Binder.bindRoot( doc, createRoot() ); } catch (MappingException me) { --- 154,158 ---- private void add(Document doc) throws Exception { try { ! Binder.bindRoot( doc, createMappings() ); } catch (MappingException me) { *************** *** 168,173 **** } ! public Root createRoot() { ! return new Root(classes, collections, tables, namedQueries); } --- 162,171 ---- } ! /** ! * Create a new <tt>Mappings</tt> to add class and collection ! * mappings to. ! */ ! public Mappings createMappings() { ! return new Mappings(classes, collections, tables, namedQueries, secondPasses); } *************** *** 413,425 **** private void secondPassCompile() throws MappingException { ! Iterator iter = collections.values().iterator(); while ( iter.hasNext() ) { ! Object coll = iter.next(); ! if (coll instanceof Set) Binder.bindSetSecondPass( (Set) coll, classes ); ! if (coll instanceof Map) Binder.bindMapSecondPass( (Map) coll, classes ); ! if (coll instanceof List) Binder.bindListSecondPass( (List) coll, classes ); ! if (coll instanceof Bag) Binder.bindCollectionSecondPass( (Bag) coll, classes ); ! if (coll instanceof Array) Binder.bindListSecondPass( (Array) coll, classes ); ! if (coll instanceof PrimitiveArray) Binder.bindListSecondPass( (PrimitiveArray) coll, classes ); } --- 411,419 ---- private void secondPassCompile() throws MappingException { ! Iterator iter = secondPasses.iterator(); while ( iter.hasNext() ) { ! Binder.SecondPass sp = (Binder.SecondPass) iter.next(); ! sp.secondPass(classes); ! iter.remove(); } *************** *** 432,436 **** while ( subIter.hasNext() ) { ! ForeignKey fk = (ForeignKey)subIter.next(); if ( fk.getReferencedTable() == null ) { PersistentClass referencedClass = (PersistentClass) classes.get( fk.getReferencedClass() ); --- 426,430 ---- while ( subIter.hasNext() ) { ! ForeignKey fk = (ForeignKey) subIter.next(); if ( fk.getReferencedTable() == null ) { PersistentClass referencedClass = (PersistentClass) classes.get( fk.getReferencedClass() ); *************** *** 454,458 **** private static final Interceptor EMPTY_INTERCEPTOR = new EmptyInterceptor(); ! public static final class EmptyInterceptor implements Interceptor, Serializable { /** * @see net.sf.hibernate.Interceptor#onDelete(Object, Serializable id, Object[], String[], Type[]) --- 448,452 ---- private static final Interceptor EMPTY_INTERCEPTOR = new EmptyInterceptor(); ! static final class EmptyInterceptor implements Interceptor, Serializable { /** * @see net.sf.hibernate.Interceptor#onDelete(Object, Serializable id, Object[], String[], Type[]) |