📄 hbmbinder.java
字号:
if ( !supportsCallable ) { throw new MappingException( "callable attribute not supported yet!" ); } return true; } return false; } private static ExecuteUpdateResultCheckStyle getResultCheckStyle(Element element, boolean callable) throws MappingException { Attribute attr = element.attribute( "check" ); if ( attr == null ) { // use COUNT as the default. This mimics the old behavior, although // NONE might be a better option moving forward in the case of callable return ExecuteUpdateResultCheckStyle.COUNT; } return ExecuteUpdateResultCheckStyle.parse( attr.getValue() ); } public static void bindUnionSubclass(Element node, UnionSubclass unionSubclass, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { bindClass( node, unionSubclass, mappings, inheritedMetas ); inheritedMetas = getMetas( node, inheritedMetas, true ); // get meta's from <subclass> if ( unionSubclass.getEntityPersisterClass() == null ) { unionSubclass.getRootClass().setEntityPersisterClass( UnionSubclassEntityPersister.class ); } Attribute schemaNode = node.attribute( "schema" ); String schema = schemaNode == null ? mappings.getSchemaName() : schemaNode.getValue(); Attribute catalogNode = node.attribute( "catalog" ); String catalog = catalogNode == null ? mappings.getCatalogName() : catalogNode.getValue(); Table denormalizedSuperTable = unionSubclass.getSuperclass().getTable(); Table mytable = mappings.addDenormalizedTable( schema, catalog, getClassTableName(unionSubclass, node, schema, catalog, denormalizedSuperTable, mappings ), unionSubclass.isAbstract() != null && unionSubclass.isAbstract().booleanValue(), getSubselect( node ), denormalizedSuperTable ); unionSubclass.setTable( mytable ); log.info( "Mapping union-subclass: " + unionSubclass.getEntityName() + " -> " + unionSubclass.getTable().getName() ); createClassProperties( node, unionSubclass, mappings, inheritedMetas ); } public static void bindSubclass(Element node, Subclass subclass, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { bindClass( node, subclass, mappings, inheritedMetas ); inheritedMetas = getMetas( node, inheritedMetas, true ); // get meta's from <subclass> if ( subclass.getEntityPersisterClass() == null ) { subclass.getRootClass() .setEntityPersisterClass( SingleTableEntityPersister.class ); } log.info( "Mapping subclass: " + subclass.getEntityName() + " -> " + subclass.getTable().getName() ); // properties createClassProperties( node, subclass, mappings, inheritedMetas ); } private static String getClassTableName( PersistentClass model, Element node, String schema, String catalog, Table denormalizedSuperTable, Mappings mappings ) { Attribute tableNameNode = node.attribute( "table" ); String logicalTableName; String physicalTableName; if ( tableNameNode == null ) { logicalTableName = StringHelper.unqualify( model.getEntityName() ); physicalTableName = mappings.getNamingStrategy().classToTableName( model.getEntityName() ); } else { logicalTableName = tableNameNode.getValue(); physicalTableName = mappings.getNamingStrategy().tableName( logicalTableName ); } mappings.addTableBinding( schema, catalog, logicalTableName, physicalTableName, denormalizedSuperTable ); return physicalTableName; } public static void bindJoinedSubclass(Element node, JoinedSubclass joinedSubclass, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { bindClass( node, joinedSubclass, mappings, inheritedMetas ); inheritedMetas = getMetas( node, inheritedMetas, true ); // get meta's from // <joined-subclass> // joined subclasses if ( joinedSubclass.getEntityPersisterClass() == null ) { joinedSubclass.getRootClass() .setEntityPersisterClass( JoinedSubclassEntityPersister.class ); } Attribute schemaNode = node.attribute( "schema" ); String schema = schemaNode == null ? mappings.getSchemaName() : schemaNode.getValue(); Attribute catalogNode = node.attribute( "catalog" ); String catalog = catalogNode == null ? mappings.getCatalogName() : catalogNode.getValue(); Table mytable = mappings.addTable( schema, catalog, getClassTableName( joinedSubclass, node, schema, catalog, null, mappings ), getSubselect( node ), false ); joinedSubclass.setTable( mytable ); bindComment(mytable, node); log.info( "Mapping joined-subclass: " + joinedSubclass.getEntityName() + " -> " + joinedSubclass.getTable().getName() ); // KEY Element keyNode = node.element( "key" ); SimpleValue key = new DependantValue( mytable, joinedSubclass.getIdentifier() ); joinedSubclass.setKey( key ); key.setCascadeDeleteEnabled( "cascade".equals( keyNode.attributeValue( "on-delete" ) ) ); bindSimpleValue( keyNode, key, false, joinedSubclass.getEntityName(), mappings ); // model.getKey().setType( new Type( model.getIdentifier() ) ); joinedSubclass.createPrimaryKey(); joinedSubclass.createForeignKey(); // CHECK Attribute chNode = node.attribute( "check" ); if ( chNode != null ) mytable.addCheckConstraint( chNode.getValue() ); // properties createClassProperties( node, joinedSubclass, mappings, inheritedMetas ); } private static void bindJoin(Element node, Join join, Mappings mappings, java.util.Map inheritedMetas) throws MappingException { PersistentClass persistentClass = join.getPersistentClass(); String path = persistentClass.getEntityName(); // TABLENAME Attribute schemaNode = node.attribute( "schema" ); String schema = schemaNode == null ? mappings.getSchemaName() : schemaNode.getValue(); Attribute catalogNode = node.attribute( "catalog" ); String catalog = catalogNode == null ? mappings.getCatalogName() : catalogNode.getValue(); Table primaryTable = persistentClass.getTable(); Table table = mappings.addTable( schema, catalog, getClassTableName( persistentClass, node, schema, catalog, primaryTable, mappings ), getSubselect( node ), false ); join.setTable( table ); bindComment(table, node); Attribute fetchNode = node.attribute( "fetch" ); if ( fetchNode != null ) { join.setSequentialSelect( "select".equals( fetchNode.getValue() ) ); } Attribute invNode = node.attribute( "inverse" ); if ( invNode != null ) { join.setInverse( "true".equals( invNode.getValue() ) ); } Attribute nullNode = node.attribute( "optional" ); if ( nullNode != null ) { join.setOptional( "true".equals( nullNode.getValue() ) ); } log.info( "Mapping class join: " + persistentClass.getEntityName() + " -> " + join.getTable().getName() ); // KEY Element keyNode = node.element( "key" ); SimpleValue key = new DependantValue( table, persistentClass.getIdentifier() ); join.setKey( key ); key.setCascadeDeleteEnabled( "cascade".equals( keyNode.attributeValue( "on-delete" ) ) ); bindSimpleValue( keyNode, key, false, persistentClass.getEntityName(), mappings ); // join.getKey().setType( new Type( lazz.getIdentifier() ) ); join.createPrimaryKey(); join.createForeignKey(); // PROPERTIES Iterator iter = node.elementIterator(); while ( iter.hasNext() ) { Element subnode = (Element) iter.next(); String name = subnode.getName(); String propertyName = subnode.attributeValue( "name" ); Value value = null; 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 ( "property".equals( name ) ) { value = new SimpleValue( table ); bindSimpleValue( subnode, (SimpleValue) value, true, propertyName, mappings ); } else if ( "component".equals( name ) || "dynamic-component".equals( name ) ) { String subpath = StringHelper.qualify( path, propertyName ); value = new Component( join ); bindComponent( subnode, (Component) value, join.getPersistentClass().getClassName(), propertyName, subpath, true, false, mappings, inheritedMetas, false ); } if ( value != null ) { Property prop = createProperty( value, propertyName, persistentClass .getEntityName(), subnode, mappings, inheritedMetas ); prop.setOptional( join.isOptional() ); join.addProperty( prop ); } } // CUSTOM SQL handleCustomSQL( node, join ); } public static void bindColumns(final Element node, final SimpleValue simpleValue, final boolean isNullable, final boolean autoColumn, final String propertyPath, final Mappings mappings) throws MappingException { Table table = simpleValue.getTable(); // COLUMN(S) Attribute columnAttribute = node.attribute( "column" ); if ( columnAttribute == null ) { Iterator iter = node.elementIterator(); int count = 0; while ( iter.hasNext() ) { Element columnElement = (Element) iter.next(); if ( columnElement.getName().equals( "column" ) ) { Column column = new Column(); column.setValue( simpleValue ); column.setTypeIndex( count++ ); bindColumn( columnElement, column, isNullable ); String logicalColumnName = mappings.getNamingStrategy().logicalColumnName( columnElement.attributeValue( "name" ), propertyPath ); column.setName( mappings.getNamingStrategy().columnName( logicalColumnName ) ); if ( table != null ) { table.addColumn( column ); // table=null -> an association // - fill it in later //TODO fill in the mappings for table == null mappings.addColumnBinding( logicalColumnName, column, table ); } simpleValue.addColumn( column ); // column index bindIndex( columnElement.attribute( "index" ), table, column, mappings ); bindIndex( node.attribute( "index" ), table, column, mappings ); //column unique-key bindUniqueKey( columnElement.attribute( "unique-key" ), table, column, mappings ); bindUniqueKey( node.attribute( "unique-key" ), table, column, mappings ); } else if ( columnElement.getName().equals( "formula" ) ) { Formula formula = new Formula(); formula.setFormula( columnElement.getText() ); simpleValue.addFormula( formula ); } } } else { if ( node.elementIterator( "column" ).hasNext() ) { throw new MappingException( "column attribute may not be used together with <column> subelement" ); } if ( node.elementIterator( "formula" ).hasNext() ) { throw new MappingException( "column attribute may not be used together with <formula> subelement" ); } Column column = new Column(); column.setValue( simpleValue ); bindColumn( node, column, isNullable ); String logicalColumnName = mappings.getNamingStrategy().logicalColumnName( columnAttribute.getValue(), propertyPath ); column.setName( mappings.getNamingStrategy().columnName( logicalColumnName ) ); if ( table != null ) { table.addColumn( column ); // table=null -> an association - fill // it in later //TODO fill in the mappings for table == null mappings.addColumnBinding( logicalColumnName, column, table ); } simpleValue.addColumn( column ); bindIndex( node.attribute( "index" ), table, column, mappings ); bindUniqueKey( node.attribute( "unique-key" ), table, column, mappings ); } if ( autoColumn && simpleValue.getColumnSpan() == 0 ) { Column column = new Column(); column.setValue( simpleValue ); bindColumn( node, column, isNullable ); column.setName( mappings.getNamingStrategy().propertyToColumnName( propertyPath ) ); String logicalName = mappings.getNamingStrategy().logicalColumnName( null, propertyPath ); mappings.addColumnBinding( logicalName, column, table ); /* TODO: joinKeyColumnName & foreignKeyColumnName should be called either here or at a * slightly higer level in the stack (to get all the information we need) * Right now HbmBinder does not support the */ simpleValue.getTable().addColumn( column ); simpleValue.addColumn( column ); bindIndex( node.attribute( "index" ), table, column, mappings ); bindUniqueKey( node.attribute( "unique-key" ), table, column, mappings ); } } private static void bindIndex(Attribute indexAttribute, Table table, Column column, Mappings mappings) { if ( indexAttribute != null && table != null ) { StringTokenizer tokens = new StringTokenizer( indexAttribute.getValue(), ", " ); while ( tokens.hasMoreTokens() ) { table.getOrCreateIndex( tokens.nextToken() ).addColumn( column ); } } } private static void bindUniqueKey(Attribute uniqueKeyAttribute, Table table, Column column, Mappings mappings) { if ( uniqueKeyAttribute != null && table != null ) { StringTokenizer tokens = new StringTokenizer( uniqueKeyAttribute.getValue(), ", " ); while ( tokens.hasMoreTokens() ) { table.getOrCreateUniqueKey( tokens.nextToken() ).addColumn( column ); } } } // automatically makes a column with the default name if none is specifed by XML public static void bindSimpleValue(Element node, SimpleValue simpleValue, boolean isNullable,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -