HHH-15415 Jakarta JsonB integration to not break GraalVM native image compilation

This commit is contained in:
Sanne Grinovero 2022-07-21 19:42:01 +01:00 committed by Sanne Grinovero
parent ca2088ad39
commit d363ba6e89
2 changed files with 40 additions and 10 deletions

View File

@ -71,10 +71,9 @@ import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.stat.Statistics;
import org.hibernate.type.FormatMapper;
import org.hibernate.type.JacksonJsonFormatMapper;
import org.hibernate.type.JaxbXmlFormatMapper;
import org.hibernate.type.JsonBJsonFormatMapper;
import org.hibernate.type.jackson.JacksonIntegration;
import org.hibernate.type.jakartajson.JakartaJsonIntegration;
import static org.hibernate.cfg.AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS;
import static org.hibernate.cfg.AvailableSettings.ALLOW_REFRESH_DETACHED_ENTITY;
@ -801,15 +800,9 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
if (jsonJacksonFormatMapper != null) {
return jsonJacksonFormatMapper;
}
try {
// Force initialization of the instance
JsonBJsonFormatMapper.INSTANCE.hashCode();
return JsonBJsonFormatMapper.INSTANCE;
else {
return JakartaJsonIntegration.getJakartaJsonBFormatMapperOrNull();
}
catch (NoClassDefFoundError ex) {
// Ignore
}
return null;
}
);
}

View File

@ -0,0 +1,37 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.type.jakartajson;
import org.hibernate.type.FormatMapper;
import org.hibernate.type.JsonBJsonFormatMapper;
public final class JakartaJsonIntegration {
// Implementation note: we rely on the following two fields to be folded as constants
// when GraalVM native image is initializing them.
private static final boolean JAKARTA_JSON_AVAILABLE = ableToLoadJakartaJsonB();
private static final JsonBJsonFormatMapper JSON_FORMAT_MAPPER = JAKARTA_JSON_AVAILABLE ? new JsonBJsonFormatMapper() : null;
private JakartaJsonIntegration() {
//To not be instantiated: static helpers only
}
private static boolean ableToLoadJakartaJsonB() {
try {
JakartaJsonIntegration.class.getClassLoader().loadClass( "jakarta.json.bind.Jsonb" );
return true;
}
catch (ClassNotFoundException | LinkageError e) {
return false;
}
}
public static FormatMapper getJakartaJsonBFormatMapperOrNull() {
return JSON_FORMAT_MAPPER;
}
}