Skip to content

Commit 442c326

Browse files
committed
HHH-8113 - Persistence.createEntityManagerFactory() should run schema export if JPA properties are set
1 parent 2f40949 commit 442c326

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/DDLFormatterImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
* @author Steve Ebersole
3535
*/
3636
public class DDLFormatterImpl implements Formatter {
37+
public static final DDLFormatterImpl INSTANCE = new DDLFormatterImpl();
38+
3739
/**
3840
* Format an SQL statement using simple rules<ul>
3941
* <li>Insert newline after each comma</li>

hibernate-entitymanager/src/main/java/org/hibernate/jpa/SchemaGenAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private SchemaGenAction(String externalName) {
6868
* @throws IllegalArgumentException If the incoming value is unrecognized
6969
*/
7070
public static SchemaGenAction interpret(String value) {
71-
if ( StringHelper.isEmpty( value ) ) {
71+
if ( StringHelper.isEmpty( value ) || NONE.externalName.equals( value ) ) {
7272
// default is NONE
7373
return NONE;
7474
}

hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/schemagen/DatabaseTarget.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void acceptCreateCommands(Iterable<String> commands) {
5757

5858
for ( String command : commands ) {
5959
try {
60+
jdbcConnectionContext.logSqlStatement( command );
6061
jdbcStatement().execute( command );
6162
}
6263
catch (SQLException e) {
@@ -87,12 +88,12 @@ public void acceptDropCommands(Iterable<String> commands) {
8788

8889
for ( String command : commands ) {
8990
try {
91+
jdbcConnectionContext.logSqlStatement( command );
9092
jdbcStatement().execute( command );
9193
}
9294
catch (SQLException e) {
93-
throw new PersistenceException(
94-
"Unable to execute JPA schema generation drop command [" + command + "]"
95-
);
95+
// Just log the error because drop commands are often unsuccessful because the tables do not yet exist...
96+
log.warn( String.format( "Unable to execute JPA schema generation drop command [%s]", command ), e );
9697
}
9798
}
9899
}

hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/schemagen/JdbcConnectionContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import java.sql.Connection;
2828
import java.sql.SQLException;
2929

30+
import org.hibernate.engine.jdbc.internal.DDLFormatterImpl;
3031
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
32+
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
3133

3234
/**
3335
* Defines access to a JDBC Connection for use in Schema generation
@@ -36,10 +38,13 @@
3638
*/
3739
class JdbcConnectionContext {
3840
private final JdbcConnectionAccess jdbcConnectionAccess;
41+
private final SqlStatementLogger sqlStatementLogger;
42+
3943
private Connection jdbcConnection;
4044

41-
JdbcConnectionContext(JdbcConnectionAccess jdbcConnectionAccess) {
45+
JdbcConnectionContext(JdbcConnectionAccess jdbcConnectionAccess, SqlStatementLogger sqlStatementLogger) {
4246
this.jdbcConnectionAccess = jdbcConnectionAccess;
47+
this.sqlStatementLogger = sqlStatementLogger;
4348
}
4449

4550
public Connection getJdbcConnection() {
@@ -64,4 +69,8 @@ public void release() {
6469
}
6570
}
6671
}
72+
73+
public void logSqlStatement(String sqlStatement) {
74+
sqlStatementLogger.logStatement( sqlStatement, DDLFormatterImpl.INSTANCE );
75+
}
6776
}

hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/schemagen/JpaSchemaGenerator.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
4646
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
4747
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
48+
import org.hibernate.engine.jdbc.spi.JdbcServices;
49+
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
4850
import org.hibernate.internal.util.StringHelper;
4951
import org.hibernate.internal.util.config.ConfigurationHelper;
5052
import org.hibernate.jpa.AvailableSettings;
@@ -249,6 +251,8 @@ else if ( sourceType == SchemaGenSource.SCRIPTS_THEN_METADATA ) {
249251
private static JdbcConnectionContext determineAppropriateJdbcConnectionContext(
250252
Configuration hibernateConfiguration,
251253
ServiceRegistry serviceRegistry) {
254+
final SqlStatementLogger sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger();
255+
252256
// see if a specific connection has been provided:
253257
final Connection providedConnection = (Connection) hibernateConfiguration.getProperties().get(
254258
AvailableSettings.SCHEMA_GEN_CONNECTION
@@ -271,7 +275,8 @@ public void releaseConnection(Connection connection) throws SQLException {
271275
public boolean supportsAggressiveRelease() {
272276
return false;
273277
}
274-
}
278+
},
279+
sqlStatementLogger
275280
);
276281
}
277282

@@ -293,12 +298,13 @@ public void releaseConnection(Connection connection) throws SQLException {
293298
public boolean supportsAggressiveRelease() {
294299
return connectionProvider.supportsAggressiveRelease();
295300
}
296-
}
301+
},
302+
sqlStatementLogger
297303
);
298304
}
299305

300306
// otherwise, return a no-op impl
301-
return new JdbcConnectionContext( null ) {
307+
return new JdbcConnectionContext( null, sqlStatementLogger ) {
302308
@Override
303309
public Connection getJdbcConnection() {
304310
throw new PersistenceException( "No connection information supplied" );
@@ -405,13 +411,13 @@ private static void doGeneration(
405411
List<GenerationSource> dropSourceList,
406412
List<GenerationTarget> targets) {
407413
for ( GenerationTarget target : targets ) {
408-
for ( GenerationSource source : createSourceList ) {
409-
target.acceptCreateCommands( source.getCommands() );
410-
}
411-
412414
for ( GenerationSource source : dropSourceList ) {
413415
target.acceptDropCommands( source.getCommands() );
414416
}
417+
418+
for ( GenerationSource source : createSourceList ) {
419+
target.acceptCreateCommands( source.getCommands() );
420+
}
415421
}
416422
}
417423

0 commit comments

Comments
 (0)