package com.example.hibernate.model.util; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.example.hibernate.model.util.exceptions.InstanceNotFoundException; /** * Implementación xenérica do DAO en Hibernate. * * @author maria */ public class GenericDaoHibernate implements IGenericDao { private final Class entityClass; private final SessionFactory sessionFactory; @SuppressWarnings("unchecked") public GenericDaoHibernate(SessionFactory sessionFactory) { // getClass(): accedemos a la clase de la instancia que extienda esta clase // (será ProfesorDaoHibernate u XDaoHibernate) // .getGenericSuperclass(): obtenemos el tipo de la clase madre directa: // GenericDaoHibernate En el caso de que sea una clase parametrizada (con // Generics),devuelve el tipo del parámetro de tipo E: ParameterizedType: // https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getGenericSuperclass-- // .getActualTypeArguments(): devuelve un array de los tipos de los argumentos // que se le pasan al tipo parametrizado // finalmente obtenemos el nombre del tipo parametrizado: que será // Profesor this.entityClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; this.sessionFactory = sessionFactory; } protected Session getSession() { return sessionFactory.getCurrentSession(); } @Override public void create(E entity) { Session session = getSession(); session.persist(entity); } @Override public E update(E entity) { Session session = getSession(); return session.merge(entity); } @Override public boolean exists(PK id) { Session session = getSession(); return session.get(entityClass, id) != null; } @Override public E find(PK id) throws InstanceNotFoundException { Session session = getSession(); E entity = session.get(entityClass, id); if (entity == null) { throw new InstanceNotFoundException(id, entityClass.getName()); } return entity; } @Override public void remove(PK id) throws InstanceNotFoundException { Session session = getSession(); E entity = session.get(entityClass, id); if (entity == null) { throw new InstanceNotFoundException(id, entityClass.getName()); } session.remove(entity); } @Override public List findAll() { Session session = getSession(); return session.createSelectionQuery("SELECT c FROM " + entityClass.getName() + " c", entityClass) .getResultList(); } }