org.squashtest.tm.service.internal.hibernate.UberCustomPersistenceProvider.java Source code

Java tutorial

Introduction

Here is the source code for org.squashtest.tm.service.internal.hibernate.UberCustomPersistenceProvider.java

Source

/**
 *     This file is part of the Squashtest platform.
 *     Copyright (C) 2010 - 2016 Henix, henix.fr
 *
 *     See the NOTICE file distributed with this work for additional
 *     information regarding copyright ownership.
 *
 *     This is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     this software is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.squashtest.tm.service.internal.hibernate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceUnitInfo;

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;
import org.springframework.orm.jpa.persistenceunit.SmartPersistenceUnitInfo;

/**
 * <p>
 *    This class must create an EntityManagerFactory that rely on our definition of a SessionFactory. Indeed we need to provide
 * the session factory with extra functions and it must be done before the session factory is created.
 * It is done by stuffing the HibernateConfiguration with our code (@see #{SquashEntityManagerFactoryBuilderImpl}).  </p>
 * 
 * 
 * @author bsiri
 *
 */

public class UberCustomPersistenceProvider extends HibernatePersistenceProvider {

    @Override
    @SuppressWarnings("rawtypes")
    public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
        final List<String> mergedClassesAndPackages = new ArrayList<>(info.getManagedClassNames());
        if (info instanceof SmartPersistenceUnitInfo) {
            mergedClassesAndPackages.addAll(((SmartPersistenceUnitInfo) info).getManagedPackages());
        }

        return new SquashEntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(info) {
            @Override
            public List<String> getManagedClassNames() {
                return mergedClassesAndPackages;
            }
        }, properties).build();
    }

}