Spring Data JPA inheritance in repositories

前端 未结 2 367
抹茶落季
抹茶落季 2021-01-02 04:51

I have an abstract class annotated with @MappedSuperClass. There are around 15 entities that extend this abstract class (having 15 corresponding tables in the d

2条回答
  •  执念已碎
    2021-01-02 05:25

    You will have to create repository for each class. However you can keep your methods in the abstract. You'll need to provide @Query on each of the methods and use SpeL(Spring Expression Language) to add the type to the queries.

    @NoRepositoryBean
    public interface AbstractRepository 
            extends CrudRepository{
    
     @Query("select e from #{#entityName} as e from equipment where e.name = equipmentName")
     T findEquipmentByName(String equipmentName);
    
    }
    

    Then extend like the following

    @Transactional
    public interface SpecialEquipmentRepo extends AbstractRepository{
    
    }
    

提交回复
热议问题