Artur Vartanyan
Artur Vartanyan

Reputation: 839

How to make one common JpaRepository for several classes?

I am writing a Rest application using Spring-Boot, MVC, Jpa (Hibernate). I have reference classes that store, for example, the names of countries, manufacturers, types of coffee, etc. They all inherit from GeneralCatalog via inheritance @MappedSuperClass. It turns out to be too much code and interfaces when working with them. How can you reduce the number of interfaces, as well as the code in the controllers. Moreover, the methods are the same. Maybe you can create one common interface for all of them?

Class GeneralCatalog:

@Data
@MappedSuperclass
public abstract class GeneralCatalog {

    protected  @Id
    @GeneratedValue
    Long id;

    protected String name;

    protected boolean isDeleted;
}

Countries:

@Entity
@Table(name = "countries")
public class Countries extends GeneralCatalog{
}

...

All classes are also inherited without their extra lines.

CatalogService:

@Service
public class CatalogService {

    // Fields
    //
    private CountriesRepository countriesRepository;

    private ManufacturerRepository manufacturerRepository;

    private RoastingRepository roastingRepository;

    private PackagingRepository packagingRepository;

    private TeaColorRepository teaColorRepository;

    private CoffeeTypeRepository coffeeTypeRepository;

    private TeaTypeRepository teaTypeRepository;


    // Setters
    //
    @Autowired
    public void setCountriesRepository(CountriesRepository countriesRepository) {
        this.countriesRepository = countriesRepository;
    }

    @Autowired
    public void setManufacturerRepository(ManufacturerRepository manufacturerRepository) {
        this.manufacturerRepository = manufacturerRepository;
    }

    @Autowired
    public void setRoastingRepository(RoastingRepository roastingRepository) {
        this.roastingRepository = roastingRepository;
    }

    @Autowired
    public void setPackagingRepository(PackagingRepository packagingRepository) {
        this.packagingRepository = packagingRepository;
    }

    @Autowired
    public void setTeaColorRepository(TeaColorRepository teaColorRepository) {
        this.teaColorRepository = teaColorRepository;
    }

    @Autowired
    public void setCoffeeTypeRepository(CoffeeTypeRepository coffeeTypeRepository) {
        this.coffeeTypeRepository = coffeeTypeRepository;
    }

    @Autowired
    public void setTeaTypeRepository(TeaTypeRepository teaTypeRepository) {
        this.teaTypeRepository = teaTypeRepository;
    }
    //


    // METHODS
    //
    public List<Countries> findCountries(){
        return countriesRepository.findAll();
    }

    public List<Manufacturer> findManufacturers(){
        return manufacturerRepository.findAll();
    }

    public List<Roasting> findRoastings(){
        return roastingRepository.findAll();
    }

    public List<Packaging> findPackagings(){
        return packagingRepository.findAll();
    }

    public List<TeaColor> findTeaColors(){
        return teaColorRepository.findAll();
    }

    public List<CoffeeType> findCoffeeTypes(){
        return coffeeTypeRepository.findAll();
    }

    public List<TeaType> findTeaTypeS(){
        return teaTypeRepository.findAll();
    }
}

CatalogController:

@Api(value = "Catalog", tags = {"catalog"})
@RestController
@RequestMapping("/catalog")
public class CatalogController<T> {

    private CatalogService catalogService;

    @Autowired
    public CatalogService getCatalogService() {
        return catalogService;
    }

    @GetMapping("/countries")
    public List<Countries> findCountries(){
        return catalogService.findCountries();
    }

    @GetMapping("/manufacturers")
    public List<Manufacturer> findManufacturers(){
        return catalogService.findManufacturers();
    }

    @GetMapping("/roastings")
    public List<Roasting> findRoastings(){
        return catalogService.findRoastings();
    }

    @GetMapping("/packagings")
    public List<Packaging> findPackagings(){
        return catalogService.findPackagings();
    }

    @GetMapping("/teacolors")
    public List<TeaColor> findTeaColors(){
        return catalogService.findTeaColors();
    }

    @GetMapping("/coffeetypes")
    public List<CoffeeType> findCoffeeTypes(){
        return catalogService.findCoffeeTypes();
    }

    @GetMapping("/teatypes")
    public List<TeaType> findTeaTypeS(){
        return catalogService.findTeaTypeS();
    }
}

For example one interface:

public interface CountriesRepository extends JpaRepository<Countries, Long> {
}

enter image description here

enter image description here

Maybe it will be possible to generalize all this somehow?

Upvotes: 0

Views: 580

Answers (2)

Fajar AM
Fajar AM

Reputation: 116

i made methods to make simple jpa things, you can see more at https://github.com/fajaralmu/base_web_app

example :

public List<Page> getAllPages() {
        List<Page> allPages = entityRepository.findAll(Page.class);
        
        return   allPages;
    }

enter image description here

Upvotes: 1

Dominik
Dominik

Reputation: 353

Actually you can use hibernate and criteria api directly to handle your use case.

It's straight as:

session.createCriteria(entityClass).list()

Upvotes: 0

Related Questions