⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hbmbinder.java

📁 hibernate-3.1.3-all-src.zip 面向对象的访问数据库工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

	private static void handleCustomSQL(Element node, Collection model) throws MappingException {
		Element element = node.element( "sql-insert" );
		if ( element != null ) {
			boolean callable = false;
			callable = isCallable( element, true );
			model.setCustomSQLInsert( element.getTextTrim(), callable );
		}

		element = node.element( "sql-delete" );
		if ( element != null ) {
			boolean callable = false;
			callable = isCallable( element, true );
			model.setCustomSQLDelete( element.getTextTrim(), callable );
		}

		element = node.element( "sql-update" );
		if ( element != null ) {
			boolean callable = false;
			callable = isCallable( element, true );
			model.setCustomSQLUpdate( element.getTextTrim(), callable );
		}

		element = node.element( "sql-delete-all" );
		if ( element != null ) {
			boolean callable = false;
			callable = isCallable( element, true );
			model.setCustomSQLDeleteAll( element.getTextTrim(), callable );
		}
	}

	private static boolean isCallable(Element e) throws MappingException {
		return isCallable( e, true );
	}

	private static boolean isCallable(Element element, boolean supportsCallable)
			throws MappingException {
		Attribute attrib = element.attribute( "callable" );
		if ( attrib != null && "true".equals( attrib.getValue() ) ) {
			if ( !supportsCallable ) {
				throw new MappingException( "callable attribute not supported yet!" );
			}
			return true;
		}
		return false;
	}

	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 ? false : 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 ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -