📄 hbmbinder.java
字号:
} public static void bindComponent(Element node, Component component, String ownerClassName, String parentProperty, String path, boolean isNullable, boolean isEmbedded, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { component.setEmbedded( isEmbedded ); component.setMetaAttributes( getMetas( node, inheritedMetas ) ); Attribute classNode = node.attribute( "class" ); if ( classNode != null ) { component.setComponentClassName( getClassName( classNode, mappings ) ); } else if ( "dynamic-component".equals( node.getName() ) ) { component.setDynamic( true ); } else if ( isEmbedded ) { // an "embedded" component (composite ids and unique) // note that this does not handle nested components if ( component.getOwner().hasPojoRepresentation() ) { component.setComponentClassName( component.getOwner().getClassName() ); } else { component.setDynamic(true); } } else { // todo : again, how *should* this work for non-pojo entities? if ( component.getOwner().hasPojoRepresentation() ) { Class reflectedClass = reflectedPropertyClass( ownerClassName, parentProperty ); if ( reflectedClass != null ) { component.setComponentClassName( reflectedClass.getName() ); } } else { component.setDynamic(true); } } String nodeName = node.attributeValue( "node" ); if ( nodeName == null ) nodeName = node.attributeValue( "name" ); if ( nodeName == null ) nodeName = component.getOwner().getNodeName(); component.setNodeName( nodeName ); Iterator iter = node.elementIterator(); while ( iter.hasNext() ) { Element subnode = (Element) iter.next(); String name = subnode.getName(); String propertyName = getPropertyName( subnode ); 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, subpath, component.getOwner(), mappings ); mappings.addCollection( collection ); value = collection; } else if ( "many-to-one".equals( name ) || "key-many-to-one".equals( name ) ) { value = new ManyToOne( component.getTable() ); bindManyToOne( subnode, (ManyToOne) value, propertyName, isNullable, mappings ); } else if ( "one-to-one".equals( name ) ) { value = new OneToOne( component.getTable(), component.getOwner() ); bindOneToOne( subnode, (OneToOne) value, isNullable, mappings ); } else if ( "any".equals( name ) ) { value = new Any( component.getTable() ); bindAny( subnode, (Any) value, isNullable, mappings ); } else if ( "property".equals( name ) || "key-property".equals( name ) ) { value = new SimpleValue( component.getTable() ); bindSimpleValue( subnode, (SimpleValue) value, isNullable, propertyName, mappings ); } else if ( "component".equals( name ) || "dynamic-component".equals( name ) || "nested-composite-element".equals( name ) ) { value = new Component( component ); // a nested composite element bindComponent( subnode, (Component) value, component.getComponentClassName(), propertyName, subpath, isNullable, isEmbedded, mappings, inheritedMetas ); } else if ( "parent".equals( name ) ) { component.setParentProperty( propertyName ); } if ( value != null ) { component.addProperty( createProperty( value, propertyName, component .getComponentClassName(), subnode, mappings, inheritedMetas ) ); } } if ( "true".equals( node.attributeValue( "unique" ) ) ) { iter = component.getColumnIterator(); ArrayList cols = new ArrayList(); while ( iter.hasNext() ) cols.add( iter.next() ); component.getOwner().getTable().createUniqueKey( cols ); } } private static String getTypeFromXML(Element node) throws MappingException { // TODO: handle TypeDefs 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 return typeNode.getValue(); } private static void initOuterJoinFetchSetting(Element node, Fetchable model) { Attribute fetchNode = node.attribute( "fetch" ); final FetchMode fetchStyle; boolean lazy = true; if ( fetchNode == null ) { Attribute jfNode = node.attribute( "outer-join" ); if ( jfNode == null ) { if ( "many-to-many".equals( node.getName() ) ) { //NOTE SPECIAL CASE: // default to join and non-lazy for the "second join" // of the many-to-many lazy = false; fetchStyle = FetchMode.JOIN; } else if ( "one-to-one".equals( node.getName() ) ) { //NOTE SPECIAL CASE: // one-to-one constrained=false cannot be proxied, // so default to join and non-lazy lazy = ( (OneToOne) model ).isConstrained(); fetchStyle = lazy ? FetchMode.DEFAULT : FetchMode.JOIN; } else { fetchStyle = FetchMode.DEFAULT; } } else { // use old (HB 2.1) defaults if outer-join is specified String eoj = jfNode.getValue(); if ( "auto".equals( eoj ) ) { fetchStyle = FetchMode.DEFAULT; } else { boolean join = "true".equals( eoj ); fetchStyle = join ? FetchMode.JOIN : FetchMode.SELECT; } } } else { boolean join = "join".equals( fetchNode.getValue() ); //lazy = !join; fetchStyle = join ? FetchMode.JOIN : FetchMode.SELECT; } model.setFetchMode( fetchStyle ); model.setLazy(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() ); } if ( mappings.getCatalogName() != null ) { params.setProperty( PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName() ); } 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 { if ( "assigned".equals( model.getIdentifierGeneratorStrategy() ) ) { model.setNullValue( "undefined" ); } 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 createClassProperties(Element node, PersistentClass persistentClass, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { createClassProperties(node, persistentClass, mappings, inheritedMetas, null, true, true); } protected static void createClassProperties(Element node, PersistentClass persistentClass, Mappings mappings, java.util.Map inheritedMetas, UniqueKey uniqueKey, boolean mutable, boolean nullable) throws MappingException { String entityName = persistentClass.getEntityName(); Table table = persistentClass.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, propertyName, persistentClass, mappings ); mappings.addCollection( collection ); value = collection; } else if ( "many-to-one".equals( name ) ) { value = new ManyToOne( table ); bindManyToOne( subnode, (ManyToOne) value, propertyName, nullable, mappings ); } else if ( "any".equals( name ) ) { value = new Any( table ); bindAny( subnode, (Any) value, nullable, mappings ); } else if ( "one-to-one".equals( name ) ) { OneToOne oneToOne = new OneToOne( table, persistentClass ); bindOneToOne( subnode, oneToOne, true, mappings ); value = oneToOne; } else if ( "property".equals( name ) ) { value = new SimpleValue( table ); bindSimpleValue( subnode, (SimpleValue) value, nullable, propertyName, mappings ); } else if ( "component".equals( name ) || "dynamic-component".equals( name ) || "properties".equals( name ) ) { String subpath = StringHelper.qualify( entityName, propertyName ); value = new Component( persistentClass ); bindComponent( subnode, (Component) value, persistentClass.getClassName(), propertyName, subpath, true, "properties".equals( name ), mappings, inheritedMetas ); } else if ( "query-list".equals( name ) ) { value = new QueryList( table ); ( (QueryList) value ).setQueryName( subnode.attributeValue( "query-ref" ) ); } else if ( "join".equals( name ) ) { Join join = new Join(); join.setPersistentClass( persistentClass ); bindJoin( subnode, join, mappings, inheritedMetas ); persistentClass.addJoin( join ); } else if ( "subclass".equals( name ) ) { handleSubclass( persistentClass, mappings, subnode, inheritedMetas ); } else if ( "joined-subclass".equals( name ) ) { handleJoinedSubclass( persistentClass, mappings, subnode, inheritedMetas ); } else if ( "union-subclass".equals( name ) ) { handleUnionSubclass( persistentClass, mappings, subnode, inheritedMetas ); } else if ( "filter".equals( name ) ) { parseFilter( subnode, persistentClass, mappings ); } else if ( "natural-id".equals( name ) ) { UniqueKey uk = new UniqueKey(); uk.setName("_UniqueKey"); uk.setTable(table); createClassProperties( subnode, persistentClass, mappings, inheritedMetas, uk, //by default, natural-ids are "immutable" (constant) "true".equals( subnode.attributeValue("mutable") ), false); table.addUniqueKey(uk); } if ( value != null ) { Property property = createProperty( value, propertyName, persistentClass .getClassName(), subnode, mappings, inheritedMetas ); if ( !mutable ) property.setUpdateable(false); persistentClass.addProperty( property ); if ( uniqueKey!=null ) uniqueKey.addColumns( property.getColumnIterator() ); } } } private static Property createProperty(final Value value, final String propertyName, final String className, final Element subnode, final Mappings mappings, java.util.Map inheritedMetas) throws MappingException { value.setTypeUsingReflection( className, propertyName ); // this is done here 'cos we might only know the type here (ugly!) // TODO: improve this a lot: if ( value instanceof ToOne ) { ToOne toOne = (ToOne) value; String propertyRef = toOne.getReferencedPropertyName(); if ( propertyRef != null ) { mappings.addUniquePropertyReference( toOne.getReferencedEntityName(), propertyRef ); } } else if ( value instanceof Collection ) { Collection coll = (Collection) value; String propertyRef = coll.getReferencedPropertyName(); // not necessarily a *unique* property reference if ( propertyRef != null ) { mappings.addPropertyReference( coll.getOwnerEntityName(), propertyRef ); } } value.createForeignKey(); Property prop = new Property(); prop.setValue( value ); bindProperty( subnode, prop, mappings, inheritedMetas ); return prop; } private static void handleUnionSubclass(PersistentClass model, Mappings mappings, Element subnode, java.util.Map inheritedMetas) throws MappingException { UnionSubclass subclass = new UnionSubclass( model ); bindUnionSubclass( subnode, subclass, mappings, inheritedMetas ); model.addSubclass( subclass ); mappings.addClass( subcla
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -