Package com.eclecticlogic.pedal.provider.hibernate

Source Code of com.eclecticlogic.pedal.provider.hibernate.HibernateProviderAccessSpiImpl

/**
* Copyright (c) 2014 Eclectic Logic LLC
*
* 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 com.eclecticlogic.pedal.provider.hibernate;

import java.io.Serializable;
import java.sql.Connection;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.SingleTableEntityPersister;

import com.eclecticlogic.pedal.spi.ProviderAccessSpi;

/**
* Get provider specific information.
* @author kabram.
*
*/
public class HibernateProviderAccessSpiImpl implements ProviderAccessSpi {

    private EntityManagerFactory emf;


    public void setEntityManagerFactory(EntityManagerFactory emf) {
        this.emf = emf;
    }


    @Override
    public String getSchemaName() {
        SessionFactory sf = emf.unwrap(HibernateEntityManagerFactory.class).getSessionFactory();
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) sf;
        String schema = sfi.getSettings().getDefaultSchemaName();
        return schema == null ? "" : schema;
    }


    /**
     * @param entityClass Entity class for which the table name is required.
     * @return Table name if the entity class is a single table.
     */
    @Override
    public String getTableName(Class<? extends Serializable> entityClass) {
        SessionFactory sf = emf.unwrap(HibernateEntityManagerFactory.class).getSessionFactory();
        ClassMetadata metadata = sf.getClassMetadata(entityClass);
        if (metadata instanceof SingleTableEntityPersister) {
            SingleTableEntityPersister step = (SingleTableEntityPersister) metadata;
            return step.getTableName();
        } else {
            return null;
        }
    }


    @Override
    public void run(EntityManager entityManager, Consumer<Connection> work) {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> work.accept(connection));
    }


    @Override
    public <R> R exec(EntityManager entityManager, Function<Connection, R> work) {
        Session session = entityManager.unwrap(Session.class);
        return session.doReturningWork(connection -> work.apply(connection));
    }
}
TOP

Related Classes of com.eclecticlogic.pedal.provider.hibernate.HibernateProviderAccessSpiImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.