Hibernate and Spring - Dao ,Services Hibernate and Spring - Dao ,Services spring spring

Hibernate and Spring - Dao ,Services


If you are just starting development, look into Spring JPA. A service should be One-to-Many Repositories (DAO). But I would also not create all of that boilerplate code by hand anymore. Spring JPA eliminates the basic CRUD and search functions as well as pagination.

Here is a video that walks through all of the configuration for Spring, JPA, Hibernate, and finishes up with Spring Data JPA showing you all of the boilerplate code that is eliminated.

To use Spring Data JPA, your repository interface ends up being:

package com.mysampleapp.repository;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.stereotype.Repository;import com.wcfgroup.model.Employee;@Repository("employeeRepository")public interface EmployeeRepository extends JpaRepository<Employee, Long> {    Employee findBySsn(String ssn);}

And then the XML configuration to use Spring Data JPA:

<jpa:repositories base-package="com.mysampleapp.repository"/>

All of the boilerplate code is now handled for you. You no longer need to create a basic repository class with find methods and basic CRUD functions. The JpaRepository interface offers a lot of nice features and you don't have do anything for the implementation.


It´s necessary have dao, daoImpl, service, serviceImpl for each model.

  • UserDao
  • UserDaoImpl
  • UserService
  • UserServiceImpl

You can use a generic class EntityDaoImpl anf the inteface EntityDao, like this:

EntityDao:

public interface EntityDao<E> {void persist(E e) throws Exception;void remove(Object id) throws Exception;E findById(Object id) throws Exception;}

EntityDaoImpl:

public class EntityDaoImpl<E>  implements EntityDao<E> {@PersistenceContext(unitName="UnitPersistenceName")protected EntityManager entityManager;protected E instance;private Class<E> entityClass;@Transactionalpublic void persist(E e) throws HibernateException{         getEntityManager().persist(e);}    @Transactionalpublic void remove(Object id) throws Exception{         getEntityManager().remove((E)getEntityManager().find(getEntityClass(), id));}public E findById(Object id) throws Exception {         return (E)getEntityManager().find(getEntityClass(), id);    }    public EntityManager getEntityManager() {    return entityManager;}public void setEntityManager(EntityManager entityManager) throws Exception{    this.entityManager = entityManager;}    public Class<E> getEntityClass() throws Exception{         if (entityClass == null) {            Type type = getClass().getGenericSuperclass();          if (type instanceof  ParameterizedType)           {              ParameterizedType paramType = (ParameterizedType) type;              if (paramType.getActualTypeArguments().length == 2) {                    if (paramType.getActualTypeArguments()[1] instanceof  TypeVariable) {                       throw new IllegalArgumentException(                          "Can't find class using reflection");                   }                    else {                       entityClass = (Class<E>) paramType.getActualTypeArguments()[1];                  }               } else {                  entityClass = (Class<E>) paramType.getActualTypeArguments()[0];                }           } else {              throw new Exception("Can't find class using reflection");          }        }       return entityClass;   }}

And you can use like this:

public interface UserDao extends EntityDao<User> {}

and

public class UserDaoImpl extends EntityDaoImpl<User> implements UserDao{}


No there is no need to have 30 service layers or 30 dao layers.you should specify the layers not considering the Entity wise but the business functionality wise.there can be 5 or 6 entities relevant for certain function and those should be in one layer.But still you will have to have getXById(), deleteX(x), createX(x) in those redundant layers if they are required.