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

huangapple go评论65阅读模式
英文:

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> {
}

如何为多个类创建一个通用的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:

@Data
@MappedSuperclass
public abstract class GeneralCatalog {

    protected  @Id
    @GeneratedValue
    Long id;

    protected String name;

    protected boolean isDeleted;
}

Countries:

@Entity
@Table(name = &quot;countries&quot;)
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&lt;Countries&gt; findCountries(){
        return countriesRepository.findAll();
    }

    public List&lt;Manufacturer&gt; findManufacturers(){
        return manufacturerRepository.findAll();
    }

    public List&lt;Roasting&gt; findRoastings(){
        return roastingRepository.findAll();
    }

    public List&lt;Packaging&gt; findPackagings(){
        return packagingRepository.findAll();
    }

    public List&lt;TeaColor&gt; findTeaColors(){
        return teaColorRepository.findAll();
    }

    public List&lt;CoffeeType&gt; findCoffeeTypes(){
        return coffeeTypeRepository.findAll();
    }

    public List&lt;TeaType&gt; findTeaTypeS(){
        return teaTypeRepository.findAll();
    }
}

CatalogController:

@Api(value = &quot;Catalog&quot;, tags = {&quot;catalog&quot;})
@RestController
@RequestMapping(&quot;/catalog&quot;)
public class CatalogController&lt;T&gt; {

    private CatalogService catalogService;

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

    @GetMapping(&quot;/countries&quot;)
    public List&lt;Countries&gt; findCountries(){
        return catalogService.findCountries();
    }

    @GetMapping(&quot;/manufacturers&quot;)
    public List&lt;Manufacturer&gt; findManufacturers(){
        return catalogService.findManufacturers();
    }

    @GetMapping(&quot;/roastings&quot;)
    public List&lt;Roasting&gt; findRoastings(){
        return catalogService.findRoastings();
    }

    @GetMapping(&quot;/packagings&quot;)
    public List&lt;Packaging&gt; findPackagings(){
        return catalogService.findPackagings();
    }

    @GetMapping(&quot;/teacolors&quot;)
    public List&lt;TeaColor&gt; findTeaColors(){
        return catalogService.findTeaColors();
    }

    @GetMapping(&quot;/coffeetypes&quot;)
    public List&lt;CoffeeType&gt; findCoffeeTypes(){
        return catalogService.findCoffeeTypes();
    }

    @GetMapping(&quot;/teatypes&quot;)
    public List&lt;TeaType&gt; findTeaTypeS(){
        return catalogService.findTeaTypeS();
    }
}

For example one interface:

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

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

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

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;
}

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

英文:

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

example :

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

如何为多个类创建一个通用的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://go.coder-hub.com/63101866.html
匿名

发表评论

匿名网友

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

确定