简体   繁体   English

Spring Data JPA如何为实现公共接口的两个域类创建单个存储库?

[英]Spring Data JPA How to create single repository for two domain classes implementing a common interface?

I have two domain classes namely SubCategoryTierOne and SubCategoryTierTwo as described below : 我有两个域类,分别是SubCategoryTierOneSubCategoryTierTwo ,如下所述:

@Entity
public class SubCategoryTierOne implements ISubCategory
{

   /** The id. */

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;

   /** The name. */

   @NotNull
   private String name;

   /** The root. */

   @NotNull
   @ManyToOne
   private Category parent;

   /** The tier two sub categories. */
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent", cascade = CascadeType.ALL)

//Getters and Setters
}

@Entity
public class SubCategoryTierTwo implements ISubCategory
{

   /** The id. */
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;

   /** The name. */

   @NotNull
   private String name;

   /** The root. */

   @NotNull
   @ManyToOne
   private SubCategoryTierOne parent;

   /** The tier three sub categories. */
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent", cascade = CascadeType.ALL)
   private Set< SubCategoryTierThree > tierThreeSubCategories;

//Getters and Setters
}

So instead of creating seperate repositories for all these subcategories, I want to create a common repository, so all these domain classes implements a marker interface ISubCategory . 因此,我不想为所有这些子类别创建单独的存储库,而是要创建一个公共存储库,因此所有这些域类都实现了标记接口ISubCategory

I created one repository like this : 我创建了一个这样的存储库:

public interface SubCategoryTierOneRepository<T extends ISubCategory> extends JpaRepository< ISubCategory, Long >
{

}

However when I run my application I get the following error : 但是,当我运行我的应用程序时,出现以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'subCategoryRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: interface com.kyac.learning.domain.ISubCategory

Can someone tell me where I am going wrong? 有人可以告诉我我要去哪里了吗?

I understand the problem here, but are you sure you want to do something like this. 我了解这里的问题,但是您确定要执行类似的操作。 you could rethink your design. 您可以重新考虑您的设计。

If I were you, I would go with something like this. 如果我是你,我会选择这样的东西。

@Entity
public class Category  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column
    private String description;

    @Column(nullable = false, unique = true)
    private String label;

    @ManyToOne(fetch = FetchType.LAZY)
    private Category parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    private Set<Category> children = new HashSet<>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 Spring Data JPA 处理多个实体类的单个 JPA 存储库接口吗? - Can I have a single JPA repository interface handling multiple entity classes using Spring Data JPA? Spring JPA Data Repository无法为扩展CrudRepository的接口创建bean - Spring JPA Data Repository failed to create bean for interface that extends CrudRepository 如何为 spring 数据 jpa 存储库创建 bean? - How to create bean for spring data jpa repository? 如何使用 ON 子句创建 Spring 数据 jpa 存储库 - how to create a Spring data jpa repository with a clause ON 如何知道两个类是否实现一个公共接口? - How to know if two classes implement a common interface? 创建自定义存储库以使用Spring Data JPA - Create Custom Repository to Spring Data JPA 如何在实现接口的所有类中设计公共静态方法 - How to design a common static method in all classes implementing an interface Spring Data JPA:使用规范实现自定义存储库行为 - Spring Data JPA: Implementing Custom Repository Behavior with Specifications 使用Spring Data Common发布域事件时如何处理没有存储库的聚合根 - How to deal with the aggregate root without repository, when using Spring Data Common to publish domain events Spring Boot Data JPA 无法在 MockMVC 测试中自动装配存储库接口 - Spring Boot Data JPA cannot autowire repository interface in MockMVC test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM