Java tutorial
/** * Copyright(C) 2014 * NEC Corporation All rights reserved. * * No permission to use, copy, modify and distribute this software * and its documentation for any purpose is granted. * This software is provided under applicable license agreement only. */ package com.nec.crud; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import com.nec.crud.utils.TypeArgumentsUtils; /** * Implementation of <code>CrudRepository</code> using Hibernate. The * SessionFactory property is annotated for automatic resource injection. * * @param <T> * The type of the domain object for which this instance is to be * used. * @param <PK> * The type of the id of the domain object for which this instance is * to be used. * @author sondn * */ @SuppressWarnings("unchecked") public class CrudRepositoryImpl<T, PK extends Serializable> implements CrudRepository<T, PK> { protected Class<T> persistentClass = (Class<T>) TypeArgumentsUtils .getTypeArguments(CrudRepositoryImpl.class, this.getClass()).get(0); /** {@inheritDoc} */ @Override public PK save(final Session session, T entity) { return (PK) session.save(entity); } /** {@inheritDoc} */ @Override public void saveOrUpdate(final Session session, T entity) { session.saveOrUpdate(entity); } /** {@inheritDoc} */ @Override public T load(final Session session, Serializable id) { return (T) session.load(getEntityClass(), id); } /** {@inheritDoc} */ @Override public T get(final Session session, Serializable id) { return (T) session.get(getEntityClass(), id); } /** {@inheritDoc} */ @Override public void update(final Session session, T entity) { session.update(entity); } /** {@inheritDoc} */ @Override public void delete(final Session session, T entity) { session.delete(entity); } /** {@inheritDoc} */ @Override public void delete(final Session session, Serializable id) { session.delete(id); } /** {@inheritDoc} */ @Override public void refresh(final Session session, T entity) { if (entity != null && isAttached(session, entity)) { session.refresh(entity); } } /** {@inheritDoc} */ @Override public void refresh(final Session session, List<T> entities) { for (T t : entities) { refresh(session, t); } } /** {@inheritDoc} */ @Override public boolean isAttached(final Session session, T entity) { return session.contains(entity); } /** {@inheritDoc} */ @Override public void flush(final Session session) { session.flush(); } /** {@inheritDoc} */ @Override public void evict(final Session session, Object obj) { session.evict(obj); } /** {@inheritDoc} */ @Override public T merge(final Session session, T entity) { return (T) session.merge(entity); } /** {@inheritDoc} */ @Override public void persist(final Session session, T entity) { session.persist(entity); } /** {@inheritDoc} */ @Override public List<T> findAll(final Session session) { return session.createCriteria(getEntityClass()).list(); } /** {@inheritDoc} */ @Override public List<T> findByCriteria(Criteria criteria) { return criteria.list(); } /** {@inheritDoc} */ @Override public List<T> findByQuery(Query query) { return query.list(); } /** {@inheritDoc} */ @Override public T findUniqueResultByCriteria(Criteria criteria) { return (T) criteria.uniqueResult(); } /** {@inheritDoc} */ @Override public T findUniqueResultByQuery(Query query) { return (T) query.uniqueResult(); } /** {@inheritDoc} */ @Override public Query getQuery(final Session session, String query) { return session.createQuery(query); } /** {@inheritDoc} */ @Override public Query getNamedQuery(final Session session, String query) { return session.getNamedQuery(query); } /** {@inheritDoc} */ @Override public Query getSQLQuery(final Session session, String query) { return session.createSQLQuery(query); } /** {@inheritDoc} */ @Override public Criteria getCriteria(final Session session, Class<T> clazz) { return session.createCriteria(clazz); } /** {@inheritDoc} */ @Override public Class<T> getEntityClass() { return persistentClass; } }