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

The following examples show how to use org.hibernate.mapping.Table#getName() . 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: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void addColumnNameBinding(Table table, Identifier logicalName, Column column) throws DuplicateMappingException {
	TableColumnNameBinding binding = null;

	if ( columnNameBindingByTableMap == null ) {
		columnNameBindingByTableMap = new HashMap<>();
	}
	else {
		binding = columnNameBindingByTableMap.get( table );
	}

	if ( binding == null ) {
		binding = new TableColumnNameBinding( table.getName() );
		columnNameBindingByTableMap.put( table, binding );
	}

	binding.addBinding( logicalName, column );
}
 
Example 2
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 6 votes vote down vote up
protected void bindIndex(String columnName, Column column, ColumnConfig cc, Table table) {
    if (cc == null) {
        return;
    }

    Object indexObj = cc.getIndex();
    String indexDefinition = null;
    if (indexObj instanceof Boolean) {
        Boolean b = (Boolean) indexObj;
        if (b) {
            indexDefinition = table.getName() + '_' + columnName + "_idx";
        }
    }
    else if (indexObj != null) {
        indexDefinition = indexObj.toString();
    }
    if (indexDefinition == null) {
        return;
    }

    String[] tokens = indexDefinition.split(",");
    for (String index : tokens) {
        table.getOrCreateIndex(index).addColumn(column);
    }
}
 
Example 3
Source File: Mappings.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String getPhysicalColumnName(String logicalName, Table table) {
	logicalName = logicalName.toLowerCase();
	String finalName = null;
	Table currentTable = table;
	do {
		ColumnNames binding = (ColumnNames) columnNameBindingPerTable.get(currentTable);
		if (binding != null) {
			finalName = (String) binding.logicalToPhysical.get( logicalName );
		}
		String key = buildTableNameKey( currentTable.getSchema(), currentTable.getCatalog(), currentTable.getName() );
		TableDescription description = (TableDescription) tableNameBinding.get(key);
		if (description != null) currentTable = description.denormalizedSupertable;
	}
	while (finalName == null && currentTable != null);
	if (finalName == null) {
		throw new MappingException( "Unable to find column with logical name "
				+ logicalName + " in table " + table.getName() );
	}
	return finalName;
}
 
Example 4
Source File: Mappings.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String getLogicalColumnName(String physicalName, Table table) {
	String logical = null;
	Table currentTable = table;
	TableDescription description = null;
	do {
		ColumnNames binding = (ColumnNames) columnNameBindingPerTable.get(currentTable);
		if (binding != null) {
			logical = (String) binding.physicalToLogical.get( physicalName );
		}
		String key = buildTableNameKey( currentTable.getSchema(), currentTable.getCatalog(), currentTable.getName() );
		description = (TableDescription) tableNameBinding.get(key);
		if (description != null) currentTable = description.denormalizedSupertable;
	}
	while (logical == null && currentTable != null && description != null);
	if (logical == null) {
		throw new MappingException( "Unable to find logical column name from physical name "
				+ physicalName + " in table " + table.getName() );
	}
	return logical;
}
 
Example 5
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getLogicalTableName(Table ownerTable) {
	final Identifier logicalName = physicalToLogicalTableNameMap.get( ownerTable.getNameIdentifier() );
	if ( logicalName == null ) {
		throw new MappingException( "Unable to find physical table: " + ownerTable.getName() );
	}
	return logicalName.render();
}
 
Example 6
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getPhysicalColumnName(Table table, Identifier logicalName) throws MappingException {
	if ( logicalName == null ) {
		throw new MappingException( "Logical column name cannot be null" );
	}

	Table currentTable = table;
	String physicalName = null;

	while ( currentTable != null ) {
		final TableColumnNameBinding binding = columnNameBindingByTableMap.get( currentTable );
		if ( binding != null ) {
			physicalName = binding.logicalToPhysical.get( logicalName );
			if ( physicalName != null ) {
				break;
			}
		}

		if ( DenormalizedTable.class.isInstance( currentTable ) ) {
			currentTable = ( (DenormalizedTable) currentTable ).getIncludedTable();
		}
		else {
			currentTable = null;
		}
	}

	if ( physicalName == null ) {
		throw new MappingException(
				"Unable to find column with logical name " + logicalName.render() + " in table " + table.getName()
		);
	}
	return physicalName;
}
 
Example 7
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getLogicalColumnName(Table table, Identifier physicalName) throws MappingException {
	final String physicalNameString = physicalName.render( getDatabase().getJdbcEnvironment().getDialect() );
	Identifier logicalName = null;

	Table currentTable = table;
	while ( currentTable != null ) {
		final TableColumnNameBinding binding = columnNameBindingByTableMap.get( currentTable );

		if ( binding != null ) {
			logicalName = binding.physicalToLogical.get( physicalNameString );
			if ( logicalName != null ) {
				break;
			}
		}

		if ( DenormalizedTable.class.isInstance( currentTable ) ) {
			currentTable = ( (DenormalizedTable) currentTable ).getIncludedTable();
		}
		else {
			currentTable = null;
		}
	}

	if ( logicalName == null ) {
		throw new MappingException(
				"Unable to find column with physical name " + physicalNameString + " in table " + table.getName()
		);
	}
	return logicalName.render();
}
 
Example 8
Source File: Configuration.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void validateSchema(Dialect dialect, DatabaseMetadata databaseMetadata)
		throws HibernateException {
	secondPassCompile();

	String defaultCatalog = properties.getProperty( Environment.DEFAULT_CATALOG );
	String defaultSchema = properties.getProperty( Environment.DEFAULT_SCHEMA );
	
	Iterator iter = getTableMappings();
	while ( iter.hasNext() ) {
		Table table = (Table) iter.next();
		if ( table.isPhysicalTable() ) {
			

			TableMetadata tableInfo = databaseMetadata.getTableMetadata(
					table.getName(),
					( table.getSchema() == null ) ? defaultSchema : table.getSchema(),
					( table.getCatalog() == null ) ? defaultCatalog : table.getCatalog(),
							table.isQuoted());
			if ( tableInfo == null ) {
				throw new HibernateException( "Missing table: " + table.getName() );
			}
			else {
				table.validateColumns( dialect, mapping, tableInfo );
			}

		}
	}

	iter = iterateGenerators( dialect );
	while ( iter.hasNext() ) {
		PersistentIdentifierGenerator generator = (PersistentIdentifierGenerator) iter.next();
		Object key = generator.generatorKey();
		if ( !databaseMetadata.isSequence( key ) && !databaseMetadata.isTable( key ) ) {
			throw new HibernateException( "Missing sequence or table: " + key );
		}
	}
}
 
Example 9
Source File: Mappings.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void addColumnBinding(String logicalName, Column finalColumn, Table table) {
	ColumnNames binding = (ColumnNames) columnNameBindingPerTable.get(table);
	if (binding == null) {
		binding = new ColumnNames();
		columnNameBindingPerTable.put(table, binding);
	}
	String oldFinalName = (String) binding.logicalToPhysical.put(
			logicalName.toLowerCase(),
			finalColumn.getQuotedName()
	);
	if ( oldFinalName != null &&
			! ( finalColumn.isQuoted() ?
					oldFinalName.equals( finalColumn.getQuotedName() ) :
					oldFinalName.equalsIgnoreCase( finalColumn.getQuotedName() ) ) ) {
		//TODO possibly relax that
		throw new MappingException("Same logical column name referenced by different physical ones: "
				+ table.getName() + "." + logicalName + " => '" + oldFinalName + "' and '" + finalColumn.getQuotedName() + "'" );
	}
	String oldLogicalName = (String) binding.physicalToLogical.put(
			finalColumn.getQuotedName(),
			logicalName
	);
	if ( oldLogicalName != null && ! oldLogicalName.equals( logicalName ) ) {
		//TODO possibly relax that
		throw new MappingException("Same physical column represented by different logical column names: "
				+ table.getName() + "." + finalColumn.getQuotedName() + " => '" + oldLogicalName + "' and '" + logicalName + "'");
	}
}
 
Example 10
Source File: LegacyJpaImplNamingIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenLegacyJpaImplNamingStrategy_whenCreateDatabase_thenGetStrategyNames() {
    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();
    String entity = Account.class.getCanonicalName();
    PersistentClass persistentClass = metadata.getEntityBinding(entity);
    Table table = persistentClass.getTable();
    String physicalNameExpected = "Secondary_Email";
    String implicitNameExpected = "defaultEmail";
    String tableNameExpected = "Account";

    String tableNameCreated = table.getName();
    boolean columnNameIsQuoted = table
      .getColumn(3)
      .isQuoted();
    String physicalNameCreated = table
      .getColumn(3)
      .getName();
    String implicitNameCreated = table
      .getColumn(2)
      .getName();

    SoftAssertions.assertSoftly(softly -> {
        softly
          .assertThat(columnNameIsQuoted)
          .isTrue();
        softly
          .assertThat(tableNameCreated)
          .isEqualTo(tableNameExpected);
        softly
          .assertThat(physicalNameCreated)
          .isEqualTo(physicalNameExpected);
        softly
          .assertThat(implicitNameCreated)
          .isEqualTo(implicitNameExpected);
    });
}
 
Example 11
Source File: SpringBootDefaultNamingIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDefaultBootNamingStrategy_whenCreateDatabase_thenGetStrategyNames() {
    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();
    String entity = Account.class.getCanonicalName();
    PersistentClass persistentClass = metadata.getEntityBinding(entity);
    Table table = persistentClass.getTable();
    String physicalNameExpected = "secondary_email";
    String implicitNameExpected = "default_email";
    String tableNameExpected = "account";

    String tableNameCreated = table.getName();
    String physicalNameCreated = table
      .getColumn(3)
      .getName();
    String implicitNameCreated = table
      .getColumn(2)
      .getName();

    SoftAssertions softly = new SoftAssertions();
    softly
      .assertThat(tableNameCreated)
      .isEqualTo(tableNameExpected);
    softly
      .assertThat(physicalNameCreated)
      .isEqualTo(physicalNameExpected);
    softly
      .assertThat(implicitNameCreated)
      .isEqualTo(implicitNameExpected);
    softly.assertAll();
}
 
Example 12
Source File: StrategyLegacyHbmImplIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenLegacyHbmImplNamingNamingStrategy_whenCreateDatabase_thenGetStrategyNames() {
    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();
    String entity = Preference.class.getCanonicalName();
    PersistentClass persistentClass = metadata.getEntityBinding(entity);
    Collection<Table> tables = metadata
      .getDatabase()
      .getDefaultNamespace()
      .getTables();
    Table preferenceTable = persistentClass.getTable();
    String tableNameExpected = "Account_preferences";
    Table accountPreferencesTable = tables
      .stream()
      .filter(table -> table
        .getName()
        .equals(tableNameExpected))
      .findFirst()
      .get();
    String implicitNameExpected = "account";

    String implicitNameCreated = preferenceTable
      .getColumn(3)
      .getName();
    String tableNameCreated = accountPreferencesTable.getName();

    SoftAssertions.assertSoftly(softly -> {
        softly
          .assertThat(implicitNameCreated)
          .isEqualTo(implicitNameExpected);
        softly
          .assertThat(tableNameCreated)
          .isEqualTo(tableNameExpected);
    });
}
 
Example 13
Source File: Ejb3JoinColumn.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static int checkReferencedColumnsType(
		Ejb3JoinColumn[] columns,
		PersistentClass referencedEntity,
		MetadataBuildingContext context) {
	//convenient container to find whether a column is an id one or not
	Set<Column> idColumns = new HashSet<Column>();
	Iterator idColumnsIt = referencedEntity.getKey().getColumnIterator();
	while ( idColumnsIt.hasNext() ) {
		idColumns.add( (Column) idColumnsIt.next() );
	}

	boolean isFkReferencedColumnName = false;
	boolean noReferencedColumn = true;
	//build the list of potential tables
	if ( columns.length == 0 ) return NO_REFERENCE; //shortcut
	Object columnOwner = BinderHelper.findColumnOwner(
			referencedEntity,
			columns[0].getReferencedColumn(),
			context
	);
	if ( columnOwner == null ) {
		try {
			throw new MappingException(
					"Unable to find column with logical name: "
							+ columns[0].getReferencedColumn() + " in " + referencedEntity.getTable() + " and its related "
							+ "supertables and secondary tables"
			);
		}
		catch (MappingException e) {
			throw new RecoverableException( e.getMessage(), e );
		}
	}
	Table matchingTable = columnOwner instanceof PersistentClass ?
			( (PersistentClass) columnOwner ).getTable() :
			( (Join) columnOwner ).getTable();
	//check each referenced column
	for (Ejb3JoinColumn ejb3Column : columns) {
		String logicalReferencedColumnName = ejb3Column.getReferencedColumn();
		if ( StringHelper.isNotEmpty( logicalReferencedColumnName ) ) {
			String referencedColumnName;
			try {
				referencedColumnName = context.getMetadataCollector().getPhysicalColumnName(
						matchingTable,
						logicalReferencedColumnName
				);
			}
			catch (MappingException me) {
				//rewrite the exception
				throw new MappingException(
						"Unable to find column with logical name: "
								+ logicalReferencedColumnName + " in " + matchingTable.getName()
				);
			}
			noReferencedColumn = false;
			Column refCol = new Column( referencedColumnName );
			boolean contains = idColumns.contains( refCol );
			if ( !contains ) {
				isFkReferencedColumnName = true;
				break; //we know the state
			}
		}
	}
	if ( isFkReferencedColumnName ) {
		return NON_PK_REFERENCE;
	}
	else if ( noReferencedColumn ) {
		return NO_REFERENCE;
	}
	else if ( idColumns.size() != columns.length ) {
		//reference use PK but is a subset or a superset
		return NON_PK_REFERENCE;
	}
	else {
		return PK_REFERENCE;
	}
}
 
Example 14
Source File: AbstractDBUnitHibernateMemoryTest.java    From livingdoc-confluence with GNU General Public License v3.0 4 votes vote down vote up
protected String getTableName(Class<?> peristentClass) {
    Table table = getMapping(peristentClass).getTable();
    return table.getName();
}