Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate and Spring - Dao ,Services

I've been reading some tutorials and I could see that most of the MVC implementations

are based on:

1) a dao interface for example "IUserDao"

2) a dao impl of that interface - "mySimpleUserDaoImpl"

3) a service interface for persistance : "IUserService"

4) and a impl - "UserServiceImpl"

is this the best practice? i mean the reason i ask this question is because it seem redundant to have 30 services with getXById(), deleteX(x), createX(x) methods that are doing more or less the same.

please take into account that I'm using spring 3 and hibernate 4, and I decided i will ask this question before i start slamming my keyboard with code

thanks.

like image 875
Urbanleg Avatar asked May 14 '13 16:05

Urbanleg


1 Answers

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.

like image 128
bh5k Avatar answered Sep 18 '22 15:09

bh5k