All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apereo.cas.hibernate.CasHibernatePersistenceProvider Maven / Gradle / Ivy

The newest version!
package org.apereo.cas.hibernate;

import org.apereo.cas.jpa.JpaPersistenceProviderContext;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;
import org.springframework.orm.jpa.persistenceunit.SmartPersistenceUnitInfo;

import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.spi.PersistenceUnitInfo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * This is {@link CasHibernatePersistenceProvider}.
 *
 * @author Misagh Moayyed
 * @since 6.3.0
 */
@RequiredArgsConstructor
@Slf4j
public class CasHibernatePersistenceProvider extends HibernatePersistenceProvider {
    private final JpaPersistenceProviderContext providerContext;

    @Override
    public EntityManagerFactory createContainerEntityManagerFactory(final PersistenceUnitInfo info, final Map properties) {
        val filtered = CollectionUtils.intersection(info.getManagedClassNames(), providerContext.getIncludeEntityClasses());
        if (info.getManagedClassNames().isEmpty() && !providerContext.getIncludeEntityClasses().isEmpty()) {
            filtered.addAll(providerContext.getIncludeEntityClasses());
        }
        LOGGER.trace("Filtered entity classes for entity manager are [{}]", filtered);
        val mergedClassesAndPackages = new HashSet(filtered);
        if (info instanceof final SmartPersistenceUnitInfo sinfo) {
            mergedClassesAndPackages.addAll(sinfo.getManagedPackages());
        }
        val persistenceUnit = new CasPersistenceUnitInfoDescriptor(info, new ArrayList<>(mergedClassesAndPackages));
        return new EntityManagerFactoryBuilderImpl(persistenceUnit, properties).build();
    }

    @Getter
    private static final class CasPersistenceUnitInfoDescriptor extends PersistenceUnitInfoDescriptor {
        private final List managedClassNames;

        CasPersistenceUnitInfoDescriptor(final PersistenceUnitInfo info, final List mergedClassesAndPackages) {
            super(info);
            this.managedClassNames = mergedClassesAndPackages;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy