Java 以泛型作为实体类型的Spring数据存储库

Java 以泛型作为实体类型的Spring数据存储库,java,spring,jpa,spring-data,spring-data-jpa,Java,Spring,Jpa,Spring Data,Spring Data Jpa,我想在我的项目中使用Spring数据JPA存储库。通常我会创建自己的存储库 interface ProductRepository extends JPARepository<Product, Long> 我拥有扩展上述实体的所有其他实体,例如: 及 我希望任何人创建的任何存储库都只能与扩展AbstractBaseEntity的实体一起工作。所以我想这样做: 公共接口MyBaseRepository扩展 JpaRepository 然后定义两种常用方法,然后按如下方式使用: p

我想在我的项目中使用Spring数据JPA存储库。通常我会创建自己的存储库

interface ProductRepository extends JPARepository<Product, Long>
  • 我拥有扩展上述实体的所有其他实体,例如:

  • 我希望任何人创建的任何存储库都只能与扩展AbstractBaseEntity的实体一起工作。所以我想这样做:
  • 公共接口MyBaseRepository扩展 JpaRepository

    然后定义两种常用方法,然后按如下方式使用:

    public interface BreadRepository扩展MyBaseRepository

    公共接口存储库扩展了MyBaseRepository

    问题是我不能这样做。 定义MyBaseRepository时,如果使用:

    MyBaseRepository扩展了JpaRepository

    我在运行real query时出现了一个错误,即“实体没有属性类型”。 如果我只使用

    扩展JPA假设

    我得到一个错误,对象未映射。 如果我尝试

    JpaRepository

    它只是由于意外的绑定错误而失败

    我是否遗漏了任何东西,或者它在SpringDataJPA中是不可行的


    谢谢

    请看@Inheritance在我的情况下没有意义,看起来这是不可能的。非常感谢您这么快的回答!
    @MappedSuperclass
    public abstract class AbstractBaseEntity {
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Id
        @Column(name = "ID", nullable = false)
        private Long id;
        ...
    }
    
    @Entity
    @Table(name = "bread")
    public class Bread extends AbstractBaseEntity {
    @Column
    String type;
    ...
    }
    
    @Entity
    @Table(name = "butter")
    public class Butter extends AbstractBaseEntity {
    @Column
    String weight;
    ...
    }