Java Code Examples for org.hibernate.mapping.Table#addColumn()

The following examples show how to use org.hibernate.mapping.Table#addColumn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernatePropertyParser.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/**
 * 构造列
 */
private Column buildColumn(String name, int len, SimpleValue value,
						   Table table) {
	Column column = new Column();
	column.setName(name);
	column.setLength(len);
	table.addColumn(column);
	value.addColumn(column);
	return column;
}
 
Example 2
Source File: PersistentTableBulkIdStrategy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void augmentIdTableDefinition(Table idTable) {
	Column sessionIdColumn = new Column( Helper.SESSION_ID_COLUMN_NAME );
	sessionIdColumn.setSqlType( "CHAR(36)" );
	sessionIdColumn.setComment( "Used to hold the Hibernate Session identifier" );
	idTable.addColumn( sessionIdColumn );
}
 
Example 3
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
/**
 * Binds a value for the specified parameters to the meta model.
 *
 * @param type        The type of the property
 * @param simpleValue The simple value instance
 * @param nullable    Whether it is nullable
 * @param columnName  The property name
 * @param mappings    The mappings
 */
protected void bindSimpleValue(String type, SimpleValue simpleValue, boolean nullable,
                               String columnName, InFlightMetadataCollector mappings) {

    simpleValue.setTypeName(type);
    Table t = simpleValue.getTable();
    Column column = new Column();
    column.setNullable(nullable);
    column.setValue(simpleValue);
    column.setName(columnName);
    if (t != null) t.addColumn(column);

    simpleValue.addColumn(column);
}
 
Example 4
Source File: TableGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void registerExportables(Database database) {
	final Dialect dialect = database.getJdbcEnvironment().getDialect();

	final Namespace namespace = database.locateNamespace(
			qualifiedTableName.getCatalogName(),
			qualifiedTableName.getSchemaName()
	);

	Table table = namespace.locateTable( qualifiedTableName.getObjectName() );
	if ( table == null ) {
		table = namespace.createTable( qualifiedTableName.getObjectName(), false );

		// todo : note sure the best solution here.  do we add the columns if missing?  other?
		final Column segmentColumn = new ExportableColumn(
				database,
				table,
				segmentColumnName,
				StringType.INSTANCE,
				dialect.getTypeName( Types.VARCHAR, segmentValueLength, 0, 0 )
		);
		segmentColumn.setNullable( false );
		table.addColumn( segmentColumn );

		// lol
		table.setPrimaryKey( new PrimaryKey( table ) );
		table.getPrimaryKey().addColumn( segmentColumn );

		final Column valueColumn = new ExportableColumn(
				database,
				table,
				valueColumnName,
				LongType.INSTANCE
		);
		table.addColumn( valueColumn );
	}

	// allow physical naming strategies a chance to kick in
	this.renderedTableName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
			table.getQualifiedTableName(),
			dialect
	);
	table.addInitCommand( generateInsertInitCommand() );

	this.selectQuery = buildSelectQuery( dialect );
	this.updateQuery = buildUpdateQuery();
	this.insertQuery = buildInsertQuery();
}
 
Example 5
Source File: TableStructure.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void registerExportables(Database database) {
	final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
	final Dialect dialect = jdbcEnvironment.getDialect();

	final Namespace namespace = database.locateNamespace(
			logicalQualifiedTableName.getCatalogName(),
			logicalQualifiedTableName.getSchemaName()
	);

	Table table = namespace.locateTable( logicalQualifiedTableName.getObjectName() );
	if ( table == null ) {
		table = namespace.createTable( logicalQualifiedTableName.getObjectName(), false );
	}

	this.tableNameText = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			table.getQualifiedTableName(),
			dialect
	);

	this.valueColumnNameText = logicalValueColumnNameIdentifier.render( dialect );


	this.selectQuery = "select " + valueColumnNameText + " as id_val" +
			" from " + dialect.appendLockHint( LockMode.PESSIMISTIC_WRITE, tableNameText ) +
			dialect.getForUpdateString();

	this.updateQuery = "update " + tableNameText +
			" set " + valueColumnNameText + "= ?" +
			" where " + valueColumnNameText + "=?";

	ExportableColumn valueColumn = new ExportableColumn(
			database,
			table,
			valueColumnNameText,
			LongType.INSTANCE
	);
	table.addColumn( valueColumn );

	table.addInitCommand(
			new InitCommand( "insert into " + tableNameText + " values ( " + initialValue + " )" )
	);
}
 
Example 6
Source File: MultipleHiLoPerTableGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void registerExportables(Database database) {
	final Namespace namespace = database.locateNamespace(
			qualifiedTableName.getCatalogName(),
			qualifiedTableName.getSchemaName()
	);

	Table table = namespace.locateTable( qualifiedTableName.getObjectName() );
	if ( table == null ) {
		table = namespace.createTable( qualifiedTableName.getObjectName(), false );

		// todo : note sure the best solution here.  do we add the columns if missing?  other?
		table.setPrimaryKey( new PrimaryKey( table ) );

		final Column pkColumn = new ExportableColumn(
				database,
				table,
				segmentColumnName,
				StringType.INSTANCE,
				database.getDialect().getTypeName( Types.VARCHAR, keySize, 0, 0 )
		);
		pkColumn.setNullable( false );
		table.addColumn( pkColumn );
		table.getPrimaryKey().addColumn( pkColumn );

		final Column valueColumn = new ExportableColumn(
				database,
				table,
				valueColumnName,
				LongType.INSTANCE
		);
		table.addColumn( valueColumn );
	}

	final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();

	// allow physical naming strategies a chance to kick in
	tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			table.getQualifiedTableName(),
			jdbcEnvironment.getDialect()
	);

	query = "select " +
			valueColumnName +
			" from " +
			jdbcEnvironment.getDialect().appendLockHint( LockMode.PESSIMISTIC_WRITE, tableName ) +
			" where " + segmentColumnName + " = '" + segmentName + "'" +
			jdbcEnvironment.getDialect().getForUpdateString();

	update = "update " +
			tableName +
			" set " +
			valueColumnName +
			" = ? where " +
			valueColumnName +
			" = ? and " +
			segmentColumnName +
			" = '" +
			segmentName
			+ "'";

	insert = "insert into " + tableName +
			"(" + segmentColumnName + ", " + valueColumnName + ") " +
			"values('" + segmentName + "', ?)";



}
 
Example 7
Source File: AbstractMultiTableBulkIdStrategyImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final void prepare(
		JdbcServices jdbcServices,
		JdbcConnectionAccess connectionAccess,
		MetadataImplementor metadata,
		SessionFactoryOptions sessionFactoryOptions) {
	// build/get Table representation of the bulk-id tables - subclasses need hooks
	// for each:
	// 		handle DDL
	// 		build insert-select
	//		build id-subselect

	final CT context =  buildPreparationContext();

	initialize( metadata.getMetadataBuildingOptions(), sessionFactoryOptions );

	final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();

	for ( PersistentClass entityBinding : metadata.getEntityBindings() ) {
		if ( !IdTableHelper.INSTANCE.needsIdTable( entityBinding ) ) {
			continue;
		}

		final String idTableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
				determineIdTableName( jdbcEnvironment, entityBinding ),
				jdbcEnvironment.getDialect()
		);
		final Table idTable = new Table();
		idTable.setName( idTableName );
		idTable.setComment( "Used to hold id values for the " + entityBinding.getEntityName() + " entity" );

		Iterator itr = entityBinding.getTable().getPrimaryKey().getColumnIterator();
		while( itr.hasNext() ) {
			Column column = (Column) itr.next();
			idTable.addColumn( column.clone()  );
		}
		augmentIdTableDefinition( idTable );

		final TT idTableInfo = buildIdTableInfo( entityBinding, idTable, jdbcServices, metadata, context );
		idTableInfoMap.put( entityBinding.getEntityName(), idTableInfo );
	}

	finishPreparation( jdbcServices, connectionAccess, metadata, context );
}
 
Example 8
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
protected void bindEnumType(PersistentProperty property, Class<?> propertyType, SimpleValue simpleValue, String columnName) {

        PropertyConfig pc = getPropertyConfig(property);
        final PersistentEntity owner = property.getOwner();
        String typeName = getTypeName(property, getPropertyConfig(property), getMapping(owner));
        if (typeName == null) {
            Properties enumProperties = new Properties();
            enumProperties.put(ENUM_CLASS_PROP, propertyType.getName());

            String enumType = pc == null ? DEFAULT_ENUM_TYPE : pc.getEnumType();
            boolean isDefaultEnumType = enumType.equals(DEFAULT_ENUM_TYPE);
            simpleValue.setTypeName(ENUM_TYPE_CLASS);
            if (isDefaultEnumType || "string".equalsIgnoreCase(enumType)) {
                enumProperties.put(EnumType.TYPE, String.valueOf(Types.VARCHAR));
                enumProperties.put(EnumType.NAMED, Boolean.TRUE.toString());
            }
            else if("identity".equals(enumType)) {
                simpleValue.setTypeName(IdentityEnumType.class.getName());
            }
            else if (!"ordinal".equalsIgnoreCase(enumType)) {
                simpleValue.setTypeName(enumType);
            }
            simpleValue.setTypeParameters(enumProperties);
        }
        else {
            simpleValue.setTypeName(typeName);
        }

        Table t = simpleValue.getTable();
        Column column = new Column();

        if (owner.isRoot()) {
            column.setNullable(property.isNullable());
        } else {
            Mapping mapping = getMapping(owner);
            if (mapping == null || mapping.getTablePerHierarchy()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName() +
                            "] for column name [" + column.getName() + "] set to nullable");
                }
                column.setNullable(true);
            } else {
                column.setNullable(property.isNullable());
            }
        }
        column.setValue(simpleValue);
        column.setName(columnName);
        if (t != null) t.addColumn(column);

        simpleValue.addColumn(column);

        PropertyConfig propertyConfig = getPropertyConfig(property);
        if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) {
            bindIndex(columnName, column, propertyConfig.getColumns().get(0), t);
            bindColumnConfigToColumn(property, column, propertyConfig.getColumns().get(0));
        }
    }
 
Example 9
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
protected void bindSimpleValue(PersistentProperty grailsProp,
                               PersistentProperty parentProperty, SimpleValue simpleValue,
                               String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) {
    setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig);
    final PropertyConfig mappedForm = (PropertyConfig) grailsProp.getMapping().getMappedForm();
    if (mappedForm.isDerived() && !(grailsProp instanceof TenantId)) {
        Formula formula = new Formula();
        formula.setFormula(propertyConfig.getFormula());
        simpleValue.addFormula(formula);
    } else {
        Table table = simpleValue.getTable();
        boolean hasConfig = propertyConfig != null;

        String generator = hasConfig ? propertyConfig.getGenerator() : null;
        if(generator != null) {
            simpleValue.setIdentifierGeneratorStrategy(generator);
            Properties params = propertyConfig.getTypeParams();
            if(params != null) {
                Properties generatorProps = new Properties();
                generatorProps.putAll(params);

                if(generatorProps.containsKey(SEQUENCE_KEY)) {
                    generatorProps.put(SequenceStyleGenerator.SEQUENCE_PARAM,  generatorProps.getProperty(SEQUENCE_KEY));
                }
                simpleValue.setIdentifierGeneratorProperties( generatorProps );
            }
        }

        // Add the column definitions for this value/property. Note that
        // not all custom mapped properties will have column definitions,
        // in which case we still need to create a Hibernate column for
        // this value.
        List<?> columnDefinitions = hasConfig ? propertyConfig.getColumns()
                : Arrays.asList(new Object[] { null });
        if (columnDefinitions.isEmpty()) {
            columnDefinitions = Arrays.asList(new Object[] { null });
        }

        for (Object columnDefinition : columnDefinitions) {
            ColumnConfig cc = (ColumnConfig) columnDefinition;
            Column column = new Column();

            // Check for explicitly mapped column name and SQL type.
            if (cc != null) {
                if (cc.getName() != null) {
                    column.setName(cc.getName());
                }
                if (cc.getSqlType() != null) {
                    column.setSqlType(cc.getSqlType());
                }
            }

            column.setValue(simpleValue);


            if (cc != null) {
                if (cc.getLength() != -1) {
                    column.setLength(cc.getLength());
                }
                if (cc.getPrecision() != -1) {
                    column.setPrecision(cc.getPrecision());
                }
                if (cc.getScale() != -1) {
                    column.setScale(cc.getScale());
                }
                if(!mappedForm.isUniqueWithinGroup()) {
                    column.setUnique(cc.isUnique());
                }
            }

            bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName);

            if (table != null) {
                table.addColumn(column);
            }

            simpleValue.addColumn(column);
        }
    }
}