英文:
How to make one common JpaRepository for several classes?
问题
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 {
}
CatalogService:
@Service
public class CatalogService {
private CountriesRepository countriesRepository;
private ManufacturerRepository manufacturerRepository;
private RoastingRepository roastingRepository;
private PackagingRepository packagingRepository;
private TeaColorRepository teaColorRepository;
private CoffeeTypeRepository coffeeTypeRepository;
private TeaTypeRepository teaTypeRepository;
@Autowired
public void setCountriesRepository(CountriesRepository countriesRepository) {
this.countriesRepository = countriesRepository;
}
// ... (same setters for other repositories)
public List<Countries> findCountries(){
return countriesRepository.findAll();
}
// ... (same methods for other entities)
}
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();
}
// ... (same methods for other entities)
}
Example interface:
public interface CountriesRepository extends JpaRepository<Countries, Long> {
}
Maybe it will be possible to generalize all this somehow?
英文:
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> {
}
Maybe it will be possible to generalize all this somehow?
答案1
得分: 1
我创建了一些处理简单 JPA 事务的方法,你可以在 https://github.com/fajaralmu/base_web_app 查看更多。
示例:
public List<Page> getAllPages() {
List<Page> allPages = entityRepository.findAll(Page.class);
return allPages;
}
英文:
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;
}
答案2
得分: 0
实际上,您可以直接使用 Hibernate 和 Criteria API 来处理您的用例。
操作非常简单:
session.createCriteria(entityClass).list()
英文:
Actually you can use hibernate and criteria api directly to handle your use case.
It's straight as:
session.createCriteria(entityClass).list()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论