HHH-8113 - Persistence.createEntityManagerFactory() should run schema export if JPA properties are set

This commit is contained in:
Steve Ebersole 2013-04-03 11:20:52 -05:00
parent 2f40949719
commit 442c3268b3
5 changed files with 30 additions and 12 deletions

View File

@ -34,6 +34,8 @@ import org.hibernate.internal.util.StringHelper;
* @author Steve Ebersole
*/
public class DDLFormatterImpl implements Formatter {
public static final DDLFormatterImpl INSTANCE = new DDLFormatterImpl();
/**
* Format an SQL statement using simple rules<ul>
* <li>Insert newline after each comma</li>

View File

@ -68,7 +68,7 @@ public enum SchemaGenAction {
* @throws IllegalArgumentException If the incoming value is unrecognized
*/
public static SchemaGenAction interpret(String value) {
if ( StringHelper.isEmpty( value ) ) {
if ( StringHelper.isEmpty( value ) || NONE.externalName.equals( value ) ) {
// default is NONE
return NONE;
}

View File

@ -57,6 +57,7 @@ class DatabaseTarget implements GenerationTarget {
for ( String command : commands ) {
try {
jdbcConnectionContext.logSqlStatement( command );
jdbcStatement().execute( command );
}
catch (SQLException e) {
@ -87,12 +88,12 @@ class DatabaseTarget implements GenerationTarget {
for ( String command : commands ) {
try {
jdbcConnectionContext.logSqlStatement( command );
jdbcStatement().execute( command );
}
catch (SQLException e) {
throw new PersistenceException(
"Unable to execute JPA schema generation drop command [" + command + "]"
);
// Just log the error because drop commands are often unsuccessful because the tables do not yet exist...
log.warn( String.format( "Unable to execute JPA schema generation drop command [%s]", command ), e );
}
}
}

View File

@ -27,7 +27,9 @@ import javax.persistence.PersistenceException;
import java.sql.Connection;
import java.sql.SQLException;
import org.hibernate.engine.jdbc.internal.DDLFormatterImpl;
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
/**
* Defines access to a JDBC Connection for use in Schema generation
@ -36,10 +38,13 @@ import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
*/
class JdbcConnectionContext {
private final JdbcConnectionAccess jdbcConnectionAccess;
private final SqlStatementLogger sqlStatementLogger;
private Connection jdbcConnection;
JdbcConnectionContext(JdbcConnectionAccess jdbcConnectionAccess) {
JdbcConnectionContext(JdbcConnectionAccess jdbcConnectionAccess, SqlStatementLogger sqlStatementLogger) {
this.jdbcConnectionAccess = jdbcConnectionAccess;
this.sqlStatementLogger = sqlStatementLogger;
}
public Connection getJdbcConnection() {
@ -64,4 +69,8 @@ class JdbcConnectionContext {
}
}
}
public void logSqlStatement(String sqlStatement) {
sqlStatementLogger.logStatement( sqlStatement, DDLFormatterImpl.INSTANCE );
}
}

View File

@ -45,6 +45,8 @@ import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.jpa.AvailableSettings;
@ -249,6 +251,8 @@ public class JpaSchemaGenerator {
private static JdbcConnectionContext determineAppropriateJdbcConnectionContext(
Configuration hibernateConfiguration,
ServiceRegistry serviceRegistry) {
final SqlStatementLogger sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger();
// see if a specific connection has been provided:
final Connection providedConnection = (Connection) hibernateConfiguration.getProperties().get(
AvailableSettings.SCHEMA_GEN_CONNECTION
@ -271,7 +275,8 @@ public class JpaSchemaGenerator {
public boolean supportsAggressiveRelease() {
return false;
}
}
},
sqlStatementLogger
);
}
@ -293,12 +298,13 @@ public class JpaSchemaGenerator {
public boolean supportsAggressiveRelease() {
return connectionProvider.supportsAggressiveRelease();
}
}
},
sqlStatementLogger
);
}
// otherwise, return a no-op impl
return new JdbcConnectionContext( null ) {
return new JdbcConnectionContext( null, sqlStatementLogger ) {
@Override
public Connection getJdbcConnection() {
throw new PersistenceException( "No connection information supplied" );
@ -405,13 +411,13 @@ public class JpaSchemaGenerator {
List<GenerationSource> dropSourceList,
List<GenerationTarget> targets) {
for ( GenerationTarget target : targets ) {
for ( GenerationSource source : createSourceList ) {
target.acceptCreateCommands( source.getCommands() );
}
for ( GenerationSource source : dropSourceList ) {
target.acceptDropCommands( source.getCommands() );
}
for ( GenerationSource source : createSourceList ) {
target.acceptCreateCommands( source.getCommands() );
}
}
}