diff --git a/src/main/java/liquibase/ext/hibernate/database/HibernateSpringPackageDatabase.java b/src/main/java/liquibase/ext/hibernate/database/HibernateSpringPackageDatabase.java index 1ae71d85..98e0e07f 100644 --- a/src/main/java/liquibase/ext/hibernate/database/HibernateSpringPackageDatabase.java +++ b/src/main/java/liquibase/ext/hibernate/database/HibernateSpringPackageDatabase.java @@ -1,19 +1,23 @@ package liquibase.ext.hibernate.database; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import java.util.List; +import org.hibernate.bytecode.enhance.spi.EnhancementContext; import org.hibernate.cfg.AvailableSettings; import org.hibernate.envers.configuration.EnversSettings; import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; +import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor; import org.hibernate.jpa.boot.spi.Bootstrap; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager; import org.springframework.orm.jpa.persistenceunit.SmartPersistenceUnitInfo; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; - +import org.springframework.core.NativeDetector; import jakarta.persistence.spi.PersistenceUnitInfo; import liquibase.Scope; import liquibase.database.DatabaseConnection; @@ -101,12 +105,35 @@ protected EntityManagerFactoryBuilderImpl createEntityManagerFactoryBuilder() { map.put(EnversSettings.REVISION_FIELD_NAME,getHibernateConnection().getProperties().getProperty(EnversSettings.REVISION_FIELD_NAME,"REV")); map.put(EnversSettings.REVISION_TYPE_FIELD_NAME,getHibernateConnection().getProperties().getProperty(EnversSettings.REVISION_TYPE_FIELD_NAME,"REVTYPE")); map.put(AvailableSettings.USE_NATIONALIZED_CHARACTER_DATA, getProperty(AvailableSettings.USE_NATIONALIZED_CHARACTER_DATA)); - - EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(persistenceUnitInfo, map); + PersistenceUnitInfoDescriptor persistenceUnitInfoDescriptor = createPersistenceUnitInfoDescriptor(persistenceUnitInfo); + EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(persistenceUnitInfoDescriptor, map); return builder; } + + public PersistenceUnitInfoDescriptor createPersistenceUnitInfoDescriptor(PersistenceUnitInfo info) { + final List mergedClassesAndPackages = new ArrayList<>(info.getManagedClassNames()); + if (info instanceof SmartPersistenceUnitInfo ) { + mergedClassesAndPackages.addAll(((SmartPersistenceUnitInfo)info).getManagedPackages()); + } + return + new PersistenceUnitInfoDescriptor(info) { + @Override + public List getManagedClassNames() { + return mergedClassesAndPackages; + } + + @Override + public void pushClassTransformer(EnhancementContext enhancementContext) { + if (!NativeDetector.inNativeImage()) { + super.pushClassTransformer(enhancementContext); + } + } + }; + } + + @Override public String getShortName() { return "hibernateSpringPackage";