Java tutorial
/* * Copyright (C) 2012 Mathieu Cambillau <cambillaum@gmail.com> * * This program 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package org.cambillaum.jpapersistor.persistence.dao; import java.io.Serializable; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaQuery; import org.apache.commons.lang.Validate; import org.cambillaum.jpapersistor.persistence.entity.PersistentObject; /** * An abstract JPA implementation of {@link GenericDAO }. * * @param <IDTYPE> the id type of the entity attached to this DAO. * @param <CLASSTYPE> the type of the entity attached to this DAO. * @author Mathieu Cambillau <cambillaum@gmail.com> */ public class AbstractGenericJPADAO<CLASSTYPE extends PersistentObject<IDTYPE>, IDTYPE extends Serializable> implements GenericDAO<CLASSTYPE, IDTYPE> { private EntityManager em; private Class<CLASSTYPE> clazz; /** * Constructs an {@link AbstractGenericJPADAO } instance for the given class * using the given {@link EntityManager }. * * @param em the {@link EntityManager }. * @param clazz the entity class. */ public AbstractGenericJPADAO(EntityManager em, Class<CLASSTYPE> clazz) { this.em = em; this.clazz = clazz; Validate.notNull(this.em, "entityManager cannot be null"); Validate.notNull(this.clazz, "class cannot be null"); } @Override public CLASSTYPE save(CLASSTYPE instanceToSave) { Validate.notNull(instanceToSave, "instanceToSave cannot be null"); CLASSTYPE savedInstance = this.em.merge(instanceToSave); return savedInstance; } @Override public void update(CLASSTYPE instanceToUpdate) { Validate.notNull(instanceToUpdate, "instanceToUpdate cannot be null"); this.em.merge(instanceToUpdate); } @Override public void delete(CLASSTYPE instanceToDelete) { Validate.notNull(instanceToDelete, "instanceToDelete cannot be null"); this.em.remove(instanceToDelete); } @Override public CLASSTYPE findById(IDTYPE id) { Validate.notNull(id, "id cannot be null"); CLASSTYPE retrievedInstance = this.em.find(this.clazz, id); return retrievedInstance; } @Override public List<CLASSTYPE> findAll() { CriteriaQuery<CLASSTYPE> criteriaQuery = this.em.getCriteriaBuilder().createQuery(this.clazz); criteriaQuery.from(this.clazz); List<CLASSTYPE> retrievedInstances = findMany(criteriaQuery); return retrievedInstances; } @Override public CLASSTYPE findOne(TypedQuery<CLASSTYPE> query) { Validate.notNull(query, "query cannot be null"); CLASSTYPE retrievedInstance = query.getSingleResult(); return retrievedInstance; } @Override public List<CLASSTYPE> findMany(TypedQuery<CLASSTYPE> query) { Validate.notNull(query, "query cannot be null"); List<CLASSTYPE> retrievedInstances = query.getResultList(); return retrievedInstances; } @Override public CLASSTYPE findOne(CriteriaQuery<CLASSTYPE> criteriaQuery) { Validate.notNull(criteriaQuery, "criteriaQuery cannot be null"); TypedQuery<CLASSTYPE> query = this.em.createQuery(criteriaQuery); CLASSTYPE retrievedInstance = findOne(query); return retrievedInstance; } @Override public List<CLASSTYPE> findMany(CriteriaQuery<CLASSTYPE> criteriaQuery) { Validate.notNull(criteriaQuery, "criteriaQuery cannot be null"); TypedQuery<CLASSTYPE> query = this.em.createQuery(criteriaQuery); List<CLASSTYPE> retrievedInstances = findMany(query); return retrievedInstances; } }