Fix JPA setup in Nomulus tool (#780)

* Fix JPA setup in Nomulus tool

Hibernate unnecessarily scans third-party classes in the Nomulus tool,
hitting a bug and fails to set up.

In this change we properly configured persistence.xml to include the orm mapping file (orm.xml) and disable 
auto detection, and provided a custom (NOOP) scanner
to work around Hibernate scanner bugs.

Also improved on the :core:registryIntegrationTest task to test for
JPA setup as well as dependency-packaging.
This commit is contained in:
Weimin Yu 2020-08-26 09:51:33 -04:00 committed by GitHub
parent b8e9d56aec
commit 20e04d2afd
4 changed files with 218 additions and 166 deletions

View file

@ -0,0 +1,45 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.persistence;
import com.google.common.collect.ImmutableSet;
import org.hibernate.boot.archive.internal.StandardArchiveDescriptorFactory;
import org.hibernate.boot.archive.scan.internal.ScanResultImpl;
import org.hibernate.boot.archive.scan.internal.StandardScanner;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
import org.hibernate.boot.archive.scan.spi.ScanOptions;
import org.hibernate.boot.archive.scan.spi.ScanParameters;
import org.hibernate.boot.archive.scan.spi.ScanResult;
import org.hibernate.boot.archive.scan.spi.Scanner;
/**
* A do-nothing {@link Scanner} for Hibernate that works around bugs in Hibernate's default
* implementation. This is required for the Nomulus tool.
*
* <p>Please refer to <a href="../../../../resources/META-INF/persistence.xml">persistence.xml</a>
* for more information.
*/
public class NoopJpaEntityScanner extends StandardScanner {
public NoopJpaEntityScanner() {
super(StandardArchiveDescriptorFactory.INSTANCE);
}
@Override
public ScanResult scan(
ScanEnvironment environment, ScanOptions options, ScanParameters parameters) {
return new ScanResultImpl(ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of());
}
}

View file

@ -112,8 +112,6 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
// Create all command instances. It would be preferrable to do this in the constructor, but
// JCommander mutates the command instances and doesn't reset them so we have to do it for every
// run.
// TODO(weiminyu): extract this into a standalone static method to simplify
// :core:registryToolIntegrationTest
try {
for (Map.Entry<String, ? extends Class<? extends Command>> entry : commands.entrySet()) {
Command command = entry.getValue().getDeclaredConstructor().newInstance();

View file

@ -11,14 +11,33 @@
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--
All JPA entities must be enumerated here. JPA does not support auto detection.
All JPA entity-mapping files and annotated classes must be enumerated
here. Automatic entity detection is not part of the JPA spec. Explicit
declaration makes it easier to migrate to another provider.
Note that Hibernate's auto detection functionality (hibernate.archive.autodection)
does not meet our needs. It only scans archives, not the 'classes' folders. So we
are left with two options:
* Move tests to another (sub)project. This is not a big problem, but feels unnatural.
* Use Hibernate's ServiceRegistry for bootstrapping (not JPA-compliant)
Although Hibernate provides the auto detection functionality (configured by
the hibernate.archive.autodetection property), it relies on a fragile
scanner that can be broken by certain classes. For example, in the uber jar
for the Nomulus tool, a repackaged Guava class ( {@code
com.google.appengine.repackaged.com.google.common.html.LinkDetector})
from appengine-api-1.0-sdk:1.9.81 can break the scanner in
hibernate-core:5.4.17.Final. The large number of third-party classes also
makes JPA setup noticeably slower in the tool.
When auto detection is enabled in Hibernate, we also need a separate
persistence.xml for tests. See <a
href="https://stackoverflow.com/questions/61127082/hibernate-doesnt-find-entities-in-test">
this webpage</a> for an example.
Because of the reasons above, we disable auto detection in Hibernate.
When auto detection is disabled, Hibernate still invokes the scanner which always
goes over the archive that has this file. We need to override the default scanner
with an NOOP one for Nomulus tool.
-->
<mapping-file>META-INF/orm.xml</mapping-file>
<class>google.registry.model.billing.BillingEvent$Cancellation</class>
<class>google.registry.model.billing.BillingEvent$OneTime</class>
<class>google.registry.model.billing.BillingEvent$Recurring</class>
@ -82,5 +101,12 @@
<!-- TODO(weiminyu): check out application-layer validation. -->
<validation-mode>NONE</validation-mode>
<properties>
<!-- Disables auto detection. -->
<property name="hibernate.archive.autodetection" value=""/>
<!-- NOOP scanner needed for Nomulus tool. -->
<property name="hibernate.archive.scanner"
value="google.registry.persistence.NoopJpaEntityScanner"/>
</properties>
</persistence-unit>
</persistence>