0
    package com.lhoussaine.springjsfjpa.entities;

    @Table(name="address")
    @Entity
    public class Address {

             @Id
             @GeneratedValue(strategy = GenerationType.AUTO)
             private int id;
             private String streetNumber;
             private String streetName;
             private String city;
             getter/setter 
    }

and I Have 30 entities.

Now repositories:

package com.lhoussaine.springjsfjpa.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.repository.annotation.RestResource;
import com.lhoussaine.springjsfjpa.entities.Address;

@RestResource(rel="address", path="address")
public interface AddressRepository extends JpaRepository<Address,Integer> { 

}

Here I dont need to implemente CRUD operation! thanks to spring-data-jpa! And I want same standard for controller and services:

public interface IAddressService {
}


package com.lhoussaine.springjsfjpa.services.generic;

import java.util.List;

public abstract class GenericService<T,K> {         
     public abstract T create(T saved);     
     public abstract void remove(T deleted);    
     public abstract T findById(K id) ;     
     public abstract List<T> findAll();
     public abstract T removeById(K id); 
}


package com.lhoussaine.springjsfjpa.services.impl;

 @Service 
 @Transactional 
 public class AddressService extends GenericService<Address, Integer> implements IAddressService {  
    @Autowired private AddressRepository iaddressRepository;

    public Address create(Address saved) {      
               Address address=saved;
        return iaddressRepository.save(address);    
       }

     public void remove(Address deleted) {
        iaddressRepository.delete(deleted);     
       }

     public Address findById(Integer id) {      
                return iaddressRepository.findOne(id);  
       }

     public List<Address> findAll() {       
               return iaddressRepository.findAll();     
        }

     public Address removeById(Integer id) {        
               Address addr= iaddressRepository.findOne(id);        
               if(addr!=null){
           iaddressRepository.delete(addr);         
               }        
               return addr;
       }
 }

Now the question is: with controller how I do? Develop a controller for each class? knowing that I have 30 service classes. Is there something approaching the same standard such as Spring Data JPA but for services and controller? As you see with services classes! I'm obliged to make GenericService classes and create an interface for each class that I have in my package entities.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25

2 Answers2

1

The controllers and the services should not be generic. Although it's understandable that every entity in your app could be created or found by ID, the services should only have the methods needed to implement the business logic of the app.

And the controllers should be created to implement the UI layer of your app. So, once you have a specification (or a clear idea in mind) of how a specific page of your application should look like and work, then implement te controller and the services to implement this page.

If your app is so generic that all it does is create, update and delete rows in tables, then you don't need to implement anything: a generic database web interface like PHPMyAdmin will do.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Merci Jean-baptiste.:) of course i know that! but juste to know is there is a framework which can make life easy. thank you very much! – Julien Castanier Nov 14 '13 at 11:34
  • You know that, but still you're looking for generic services, and generic controllers. You shouldn't have generic services and generic controllers. You shouldn't have one servce per entity class and one controller per entity class. – JB Nizet Nov 14 '13 at 12:15
0

You can use generic service and controllers only without annotations, i.e with XML configuration. For controllers you also have to set Map<[methodName],org.springframework.web.bind.annotation.RequestMapping> for each controller and extend (override) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping to use it. See example https://sites.google.com/site/beigeerp/home/spring-generic-controller