Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.epam.dao.hiberhate;
- import java.lang.reflect.ParameterizedType;
- import org.hibernate.SessionFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import com.epam.dao.BaseDao;
- @SuppressWarnings("unchecked")
- public abstract class GenericDao<T> implements BaseDao<T> {
- @Autowired
- protected SessionFactory sessionFactory;
- private Class<?> getEntityClass() {
- return ((Class) ((ParameterizedType) getClass().getGenericSuperclass())
- .getActualTypeArguments()[0]);
- }
- public T get(Integer id) {
- return (T) sessionFactory.getCurrentSession()
- .get( getEntityClass(), id );
- }
- public T save(T entity) {
- Integer id = (Integer) sessionFactory.getCurrentSession().save( entity );
- return (T) sessionFactory.getCurrentSession()
- .get( getEntityClass(), id );
- }
- public T update(T entity) {
- sessionFactory.getCurrentSession().update( entity );
- return entity;
- }
- public void delete(T entity) {
- sessionFactory.getCurrentSession().delete( entity );
- }
- public void delete(Integer id) {
- T entity = (T) sessionFactory.getCurrentSession().get(
- getEntityClass(), id );
- sessionFactory.getCurrentSession().delete( entity );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement