如何为多个类创建一个通用的JpaRepository?

huangapple 未分类评论53阅读模式
英文:

How to make one common JpaRepository for several classes?

问题

Class GeneralCatalog:

  1. @Data
  2. @MappedSuperclass
  3. public abstract class GeneralCatalog {
  4. protected @Id @GeneratedValue Long id;
  5. protected String name;
  6. protected boolean isDeleted;
  7. }

Countries:

  1. @Entity
  2. @Table(name = "countries")
  3. public class Countries extends GeneralCatalog {
  4. }

CatalogService:

  1. @Service
  2. public class CatalogService {
  3. private CountriesRepository countriesRepository;
  4. private ManufacturerRepository manufacturerRepository;
  5. private RoastingRepository roastingRepository;
  6. private PackagingRepository packagingRepository;
  7. private TeaColorRepository teaColorRepository;
  8. private CoffeeTypeRepository coffeeTypeRepository;
  9. private TeaTypeRepository teaTypeRepository;
  10. @Autowired
  11. public void setCountriesRepository(CountriesRepository countriesRepository) {
  12. this.countriesRepository = countriesRepository;
  13. }
  14. // ... (same setters for other repositories)
  15. public List<Countries> findCountries(){
  16. return countriesRepository.findAll();
  17. }
  18. // ... (same methods for other entities)
  19. }

CatalogController:

  1. @Api(value = "Catalog", tags = {"catalog"})
  2. @RestController
  3. @RequestMapping("/catalog")
  4. public class CatalogController<T> {
  5. private CatalogService catalogService;
  6. @Autowired
  7. public CatalogService getCatalogService() {
  8. return catalogService;
  9. }
  10. @GetMapping("/countries")
  11. public List<Countries> findCountries(){
  12. return catalogService.findCountries();
  13. }
  14. // ... (same methods for other entities)
  15. }

Example interface:

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

如何为多个类创建一个通用的JpaRepository?

如何为多个类创建一个通用的JpaRepository?

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:

  1. @Data
  2. @MappedSuperclass
  3. public abstract class GeneralCatalog {
  4. protected @Id
  5. @GeneratedValue
  6. Long id;
  7. protected String name;
  8. protected boolean isDeleted;
  9. }

Countries:

  1. @Entity
  2. @Table(name = &quot;countries&quot;)
  3. public class Countries extends GeneralCatalog{
  4. }

...

All classes are also inherited without their extra lines.

CatalogService:

  1. @Service
  2. public class CatalogService {
  3. // Fields
  4. //
  5. private CountriesRepository countriesRepository;
  6. private ManufacturerRepository manufacturerRepository;
  7. private RoastingRepository roastingRepository;
  8. private PackagingRepository packagingRepository;
  9. private TeaColorRepository teaColorRepository;
  10. private CoffeeTypeRepository coffeeTypeRepository;
  11. private TeaTypeRepository teaTypeRepository;
  12. // Setters
  13. //
  14. @Autowired
  15. public void setCountriesRepository(CountriesRepository countriesRepository) {
  16. this.countriesRepository = countriesRepository;
  17. }
  18. @Autowired
  19. public void setManufacturerRepository(ManufacturerRepository manufacturerRepository) {
  20. this.manufacturerRepository = manufacturerRepository;
  21. }
  22. @Autowired
  23. public void setRoastingRepository(RoastingRepository roastingRepository) {
  24. this.roastingRepository = roastingRepository;
  25. }
  26. @Autowired
  27. public void setPackagingRepository(PackagingRepository packagingRepository) {
  28. this.packagingRepository = packagingRepository;
  29. }
  30. @Autowired
  31. public void setTeaColorRepository(TeaColorRepository teaColorRepository) {
  32. this.teaColorRepository = teaColorRepository;
  33. }
  34. @Autowired
  35. public void setCoffeeTypeRepository(CoffeeTypeRepository coffeeTypeRepository) {
  36. this.coffeeTypeRepository = coffeeTypeRepository;
  37. }
  38. @Autowired
  39. public void setTeaTypeRepository(TeaTypeRepository teaTypeRepository) {
  40. this.teaTypeRepository = teaTypeRepository;
  41. }
  42. //
  43. // METHODS
  44. //
  45. public List&lt;Countries&gt; findCountries(){
  46. return countriesRepository.findAll();
  47. }
  48. public List&lt;Manufacturer&gt; findManufacturers(){
  49. return manufacturerRepository.findAll();
  50. }
  51. public List&lt;Roasting&gt; findRoastings(){
  52. return roastingRepository.findAll();
  53. }
  54. public List&lt;Packaging&gt; findPackagings(){
  55. return packagingRepository.findAll();
  56. }
  57. public List&lt;TeaColor&gt; findTeaColors(){
  58. return teaColorRepository.findAll();
  59. }
  60. public List&lt;CoffeeType&gt; findCoffeeTypes(){
  61. return coffeeTypeRepository.findAll();
  62. }
  63. public List&lt;TeaType&gt; findTeaTypeS(){
  64. return teaTypeRepository.findAll();
  65. }
  66. }

CatalogController:

  1. @Api(value = &quot;Catalog&quot;, tags = {&quot;catalog&quot;})
  2. @RestController
  3. @RequestMapping(&quot;/catalog&quot;)
  4. public class CatalogController&lt;T&gt; {
  5. private CatalogService catalogService;
  6. @Autowired
  7. public CatalogService getCatalogService() {
  8. return catalogService;
  9. }
  10. @GetMapping(&quot;/countries&quot;)
  11. public List&lt;Countries&gt; findCountries(){
  12. return catalogService.findCountries();
  13. }
  14. @GetMapping(&quot;/manufacturers&quot;)
  15. public List&lt;Manufacturer&gt; findManufacturers(){
  16. return catalogService.findManufacturers();
  17. }
  18. @GetMapping(&quot;/roastings&quot;)
  19. public List&lt;Roasting&gt; findRoastings(){
  20. return catalogService.findRoastings();
  21. }
  22. @GetMapping(&quot;/packagings&quot;)
  23. public List&lt;Packaging&gt; findPackagings(){
  24. return catalogService.findPackagings();
  25. }
  26. @GetMapping(&quot;/teacolors&quot;)
  27. public List&lt;TeaColor&gt; findTeaColors(){
  28. return catalogService.findTeaColors();
  29. }
  30. @GetMapping(&quot;/coffeetypes&quot;)
  31. public List&lt;CoffeeType&gt; findCoffeeTypes(){
  32. return catalogService.findCoffeeTypes();
  33. }
  34. @GetMapping(&quot;/teatypes&quot;)
  35. public List&lt;TeaType&gt; findTeaTypeS(){
  36. return catalogService.findTeaTypeS();
  37. }
  38. }

For example one interface:

  1. public interface CountriesRepository extends JpaRepository&lt;Countries, Long&gt; {
  2. }

如何为多个类创建一个通用的JpaRepository?

如何为多个类创建一个通用的JpaRepository?

Maybe it will be possible to generalize all this somehow?

答案1

得分: 1

我创建了一些处理简单 JPA 事务的方法,你可以在 https://github.com/fajaralmu/base_web_app 查看更多。

示例:

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

如何为多个类创建一个通用的JpaRepository?

英文:

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

example :

  1. public List&lt;Page&gt; getAllPages() {
  2. List&lt;Page&gt; allPages = entityRepository.findAll(Page.class);
  3. return allPages;
  4. }

如何为多个类创建一个通用的JpaRepository?

答案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()

huangapple
  • 本文由 发表于 2020年7月26日 23:04:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/63101866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定