HHH-8183 Added setting to enable synonyms

This commit is contained in:
Brett Meyer 2013-04-15 13:00:53 -04:00
parent 15a0868bd7
commit 1af9724692
4 changed files with 50 additions and 12 deletions

View File

@ -23,6 +23,8 @@
*/
package org.hibernate.cfg;
import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
/**
* @author Steve Ebersole
*/
@ -638,4 +640,12 @@ public interface AvailableSettings {
public static final String JACC_CONTEXT_ID = "hibernate.jacc_context_id";
public static final String JACC_PREFIX = "hibernate.jacc";
public static final String JACC_ENABLED = "hibernate.jacc.enabled";
/**
* If enabled, allows {@link org.hibernate.tool.hbm2ddl.DatabaseMetadata} to
* support synonyms during schema update and validations. Due to the
* possibility that this would return duplicate tables (especially in
* Oracle), this is disabled by default.
*/
public static final String ENABLE_SYNONYMS = "hibernate.synonyms";
}

View File

@ -34,20 +34,25 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.jboss.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.exception.spi.SQLExceptionConverter;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.mapping.Table;
import org.jboss.logging.Logger;
/**
* JDBC database metadata
* @author Christoph Sturm, Teodor Danciu
*/
/**
* @author Brett Meyer
*/
public class DatabaseMetadata {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, DatabaseMetaData.class.getName());
@ -59,19 +64,42 @@ public class DatabaseMetadata {
private DatabaseMetaData meta;
private SQLExceptionConverter sqlExceptionConverter;
private final String[] types;
/**
* @deprecated Use {@link #DatabaseMetadata(Connection, Dialect, Configuration)} instead
*/
@Deprecated
public DatabaseMetadata(Connection connection, Dialect dialect) throws SQLException {
this(connection, dialect, true);
this(connection, dialect, null, true);
}
/**
* @deprecated Use {@link #DatabaseMetadata(Connection, Dialect, Configuration, boolean)} instead
*/
@Deprecated
public DatabaseMetadata(Connection connection, Dialect dialect, boolean extras) throws SQLException {
this(connection, dialect, null, extras);
}
public DatabaseMetadata(Connection connection, Dialect dialect, Configuration config) throws SQLException {
this(connection, dialect, config, true);
}
public DatabaseMetadata(Connection connection, Dialect dialect, Configuration config, boolean extras)
throws SQLException {
sqlExceptionConverter = dialect.buildSQLExceptionConverter();
meta = connection.getMetaData();
this.extras = extras;
initSequences(connection, dialect);
initSequences( connection, dialect );
if ( config != null
&& ConfigurationHelper.getBoolean( AvailableSettings.ENABLE_SYNONYMS, config.getProperties(), false ) ) {
types = new String[] { "TABLE", "VIEW", "SYNONYM" };
}
else {
types = new String[] { "TABLE", "VIEW" };
}
}
private static final String[] TYPES = {"TABLE", "VIEW", "SYNONYM"};
public TableMetadata getTableMetadata(String name, String schema, String catalog, boolean isQuoted) throws HibernateException {
Object identifier = identifier(catalog, schema, name);
@ -85,14 +113,14 @@ public class DatabaseMetadata {
ResultSet rs = null;
try {
if ( (isQuoted && meta.storesMixedCaseQuotedIdentifiers())) {
rs = meta.getTables(catalog, schema, name, TYPES);
rs = meta.getTables(catalog, schema, name, types);
} else if ( (isQuoted && meta.storesUpperCaseQuotedIdentifiers())
|| (!isQuoted && meta.storesUpperCaseIdentifiers() )) {
rs = meta.getTables(
StringHelper.toUpperCase(catalog),
StringHelper.toUpperCase(schema),
StringHelper.toUpperCase(name),
TYPES
types
);
}
else if ( (isQuoted && meta.storesLowerCaseQuotedIdentifiers())
@ -101,11 +129,11 @@ public class DatabaseMetadata {
StringHelper.toLowerCase( catalog ),
StringHelper.toLowerCase(schema),
StringHelper.toLowerCase(name),
TYPES
types
);
}
else {
rs = meta.getTables(catalog, schema, name, TYPES);
rs = meta.getTables(catalog, schema, name, types);
}
while ( rs.next() ) {

View File

@ -193,7 +193,7 @@ public class SchemaUpdate {
LOG.fetchingDatabaseMetadata();
connectionHelper.prepare( true );
connection = connectionHelper.getConnection();
meta = new DatabaseMetadata( connection, dialect );
meta = new DatabaseMetadata( connection, dialect, configuration );
stmt = connection.createStatement();
}
catch ( SQLException sqle ) {

View File

@ -145,7 +145,7 @@ public class SchemaValidator {
LOG.fetchingDatabaseMetadata();
connectionHelper.prepare( false );
connection = connectionHelper.getConnection();
meta = new DatabaseMetadata( connection, dialect, false );
meta = new DatabaseMetadata( connection, dialect, configuration, false );
}
catch ( SQLException sqle ) {
LOG.unableToGetDatabaseMetadata(sqle);