Fix show-sql which stopped working (#596)

* Fix show-sql which stopped working

Made show-sql property configurable in JpaUnitTestRules.

Added a few comments on foreign key constraint behavior.
This commit is contained in:
Weimin Yu 2020-05-21 12:20:56 -04:00 committed by GitHub
parent c73d154084
commit 54f1357d83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View file

@ -119,7 +119,8 @@ public class DomainBaseSqlTest {
.transact( .transact(
() -> { () -> {
// Persist the contacts. Note that these need to be persisted before the domain // Persist the contacts. Note that these need to be persisted before the domain
// otherwise we get a foreign key constraint error. // otherwise we get a foreign key constraint error. If we ever decide to defer the
// relevant foreign key checks to commit time, then the order would not matter.
jpaTm().saveNew(contact); jpaTm().saveNew(contact);
jpaTm().saveNew(contact2); jpaTm().saveNew(contact2);
@ -127,7 +128,8 @@ public class DomainBaseSqlTest {
jpaTm().saveNew(domain); jpaTm().saveNew(domain);
// Persist the host. This does _not_ need to be persisted before the domain, // Persist the host. This does _not_ need to be persisted before the domain,
// presumably because its relationship is stored in a join table. // because only the row in the join table (DomainHost) is subject to foreign key
// constraints, and Hibernate knows to insert it after domain and host.
jpaTm().saveNew(host); jpaTm().saveNew(host);
}); });

View file

@ -28,6 +28,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import org.hibernate.cfg.Environment;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.BeforeEachCallback;
@ -180,6 +181,17 @@ public class JpaTestRules {
return this; return this;
} }
/**
* Enables logging of SQL statements.
*
* <p>SQL logging is very noisy and disabled by default. This method maybe useful when
* troubleshooting a specific test.
*/
public Builder withSqlLogging() {
withProperty(Environment.SHOW_SQL, "true");
return this;
}
/** Builds a {@link JpaIntegrationTestRule} instance. */ /** Builds a {@link JpaIntegrationTestRule} instance. */
public JpaIntegrationTestRule buildIntegrationTestRule() { public JpaIntegrationTestRule buildIntegrationTestRule() {
return new JpaIntegrationTestRule( return new JpaIntegrationTestRule(

View file

@ -41,6 +41,8 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -146,13 +148,15 @@ abstract class JpaTransactionManagerRule extends ExternalResource {
ImmutableMap properties = PersistenceModule.providesDefaultDatabaseConfigs(); ImmutableMap properties = PersistenceModule.providesDefaultDatabaseConfigs();
if (!userProperties.isEmpty()) { if (!userProperties.isEmpty()) {
// If there are user properties, create a new properties object with these added. // If there are user properties, create a new properties object with these added.
ImmutableMap.Builder builder = properties.builder(); Map<String, String> mergedProperties = Maps.newHashMap();
builder.putAll(userProperties); mergedProperties.putAll(properties);
// Forbid Hibernate push to stay consistent with flyway-based schema management. mergedProperties.putAll(userProperties);
builder.put(Environment.HBM2DDL_AUTO, "none"); properties = ImmutableMap.copyOf(mergedProperties);
builder.put(Environment.SHOW_SQL, "true");
properties = builder.build();
} }
// Forbid Hibernate push to stay consistent with flyway-based schema management.
checkState(
Objects.equals(properties.get(Environment.HBM2DDL_AUTO), "none"),
"The HBM2DDL_AUTO property must be 'none'.");
assertReasonableNumDbConnections(); assertReasonableNumDbConnections();
emf = emf =
createEntityManagerFactory( createEntityManagerFactory(