mercredi 21 juin 2023

How to access JPA Repository methods from bean object of the particular repository dynamically

I have a list of repositories that extends JPA Repository and the API needs to fetch a record of a particular table given a primary key..

I have defined a enum from where I'm able to fetch the repository class object from the given table name..

public enum DynamicEntityRepository {

    Table1(Entity1.class),
    Table2(Entity2.class),
    Table3(Entity3.class),
    Table4(Entity4.class),
   
    Class entity;

}

Now, I want to access the findById(id) method of JPA Repository of the particular table..

 DynamicEntityRepository repo = DynamicEntityRepository.valueOf(tableName);
 

 /*
   Here I want to access findById(id) of the particular repository by passing the param
*/

I have tried using reflection to invoke the findById method of the Repository class but it throws NoSuchMethodException as the Repository originally does not have findById method.. It indeed extends JPARepository's default method..

Is there any other way to solve this ?





Aucun commentaire:

Enregistrer un commentaire