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

com.evasion.dao.api.AbstractJPAGenericDAO Maven / Gradle / Ivy

There is a newer version: 2.0.0.2
Show newest version
/*
 * Document confidentiel - Diffusion interdite 
 */
package com.evasion.dao.api;

import com.evasion.EntityJPA;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.persistence.EntityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation Hibernate du GenericDAO.
 * 
 * @param 
 *            le type persistent
 * @param 
 *            le type de la clef primaire de l'objet persistent
 */
public abstract class AbstractJPAGenericDAO
        implements GenericDAO, Serializable {

    /** LOGGER */
    public static final Logger LOGGER = LoggerFactory.getLogger(AbstractJPAGenericDAO.class.getName());
    private DefaultDAO defaultDAO;
    private Class persistentClass;

    /**
     * Constructeur.
     * @param  namePU
     */
    @SuppressWarnings("unchecked")
    public AbstractJPAGenericDAO() {
        super();
        defaultDAO = new DefaultDAO();
        final ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
        LOGGER.info("Show ParameterizedType :" + type.getActualTypeArguments()[0]);
        if (type.getActualTypeArguments()[0] instanceof Class) {
            this.persistentClass = (Class) type.getActualTypeArguments()[0];
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clear() {
        this.defaultDAO.clear();
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public List findAll() {
        return  (List) this.defaultDAO.findAll(this.persistentClass);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public T findById(final PK id) {
        return  (T) this.defaultDAO.findById(this.persistentClass, id);
    }

    /**
     * {@inheritDoc}
     */
    public void flush() {
        this.defaultDAO.flush();
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public T getReference(final PK id) {
        return (T) this.defaultDAO.getReference(this.persistentClass, id);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public T merge(final T entity) {
        return (T) defaultDAO.merge(entity);
    }

    /**
     * {@inheritDoc}
     */
    public void persist(final T entity) {
        this.defaultDAO.persist(entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void remove(T entity) {
        this.defaultDAO.remove(entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public EntityManager getEntityManager() {
        return this.defaultDAO.getEntityManager();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setEntityManager(EntityManager em) {
        this.defaultDAO.setEntityManager(em);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy