Permalink
Browse files

HHH-8113 - Persistence.createEntityManagerFactory() should run schema…

… export if JPA properties are set
  • Loading branch information...
1 parent 2f40949 commit 442c3268b35cdecd6b35c51a1527a0885aba1477 @sebersole sebersole committed Apr 3, 2013
@@ -34,6 +34,8 @@
* @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>
@@ -68,7 +68,7 @@ private SchemaGenAction(String externalName) {
* @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;
}
@@ -57,6 +57,7 @@ public void acceptCreateCommands(Iterable<String> commands) {
for ( String command : commands ) {
try {
+ jdbcConnectionContext.logSqlStatement( command );
jdbcStatement().execute( command );
}
catch (SQLException e) {
@@ -87,12 +88,12 @@ public void acceptDropCommands(Iterable<String> commands) {
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 );
}
}
}
@@ -27,7 +27,9 @@
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 @@
*/
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 @@ public void release() {
}
}
}
+
+ public void logSqlStatement(String sqlStatement) {
+ sqlStatementLogger.logStatement( sqlStatement, DDLFormatterImpl.INSTANCE );
+ }
}
@@ -45,6 +45,8 @@
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 @@ else if ( sourceType == SchemaGenSource.SCRIPTS_THEN_METADATA ) {
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 void releaseConnection(Connection connection) throws SQLException {
public boolean supportsAggressiveRelease() {
return false;
}
- }
+ },
+ sqlStatementLogger
);
}
@@ -293,12 +298,13 @@ public void releaseConnection(Connection connection) throws SQLException {
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 @@ private static void doGeneration(
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() );
+ }
}
}

0 comments on commit 442c326

Please sign in to comment.