GenericDao.java 3.74 KB
package net.ziemers.swxercise.db.dao;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.enterprise.inject.Alternative;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import net.ziemers.swxercise.db.BaseEntity;

/**
 * Stellt generische Persistenz-Funktionalität zur Verfügung. Wird normalerweise von den
 * spazialisierten Data Access Objects geerbt.
 *
 * Copyright (c) 2017 Dipl.-Inform. Thomas Ziemer
 *
 * This file is part of swXercise.
 *
 * swXercise is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * swXercise 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with swXercise.
 * If not, see <http://www.gnu.org/licenses/>.
 */
@Stateless
// diese Bean ist nur die Alternative zur Ermittlung des Entity Managers
// Quelle: https://stackoverflow.com/questions/10185976/cdi-ambiguous-dependencies
@Alternative
public class GenericDao {

    /*
     * Der Entity Manager wird per CDI injiziert, damit er auch im Testkontext mit dem CDI-Runner funktioniert.
     * Im JEE-Container "zieht" dann die Produktion des Entity Managers in der Klasse "EntityMangerProducer".
     */
    @Inject
    protected EntityManager entityManager;

    /**
     * Speichert die gegebene Entität.
     *
     * @see EntityManager#persist(Object)
     * @param entity die zu speichernde Entität
     * @param <T> Generic für den Entity-Typ.
     * @return die Id der persistierten Entität
     */
    public <T extends BaseEntity> Long save(final T entity) {
        entityManager.persist(entity);
        return entity.getId();
    }

    public <T extends BaseEntity> T saveOrUpdate(final T entity) {
        return entityManager.merge(entity);
    }

    public <T extends BaseEntity> T remove(Class<T> entityType, Long primaryKey) {
        // In order to delete an object from the database, it has to first be
        // retrieved and then in an active transaction, it can be deleted using
        // the remove() method.
        final T entity = findById(entityType, primaryKey);

        // An "IllegalArgumentException" is thrown by remove(), if the argument
        // is not an instance of an entity class or if it is a detached entity.
        entityManager.remove(entity);
        return entity;
    }

    /**
     * @param entityType Entity-Typ.
     * @param primaryKey Id
     * @param <T> Generic für Entity-Typ
     * @return die Entität mit gegebener Id oder <code>null</code>, wenn keine gefunden
     */
    public <T extends BaseEntity> T findById(Class<T> entityType, Long primaryKey) {
        final T entity = entityManager.find(entityType, primaryKey);
        return entity;
    }

    /**
     * @param entityType Entity-Typ.
     * @param <T> Generic für den Entity-Typ.
     * @return alle Entitäten vom gegebenen Typ
     */
    public <T extends BaseEntity> Collection<T> findAll(Class<T> entityType) {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<T> cq = cb.createQuery(entityType);
        Root<T> root = cq.from(entityType);
        CriteriaQuery<T> selectAll = cq.select(root);
        TypedQuery<T> query = entityManager.createQuery(selectAll);
        return query.getResultList();
    }

}