![](/img/trans.png)
[英]Can I have a single JPA repository interface handling multiple entity classes using Spring Data JPA?
[英]Spring Data JPA How to create single repository for two domain classes implementing a common interface?
我有两个域类,分别是SubCategoryTierOne和SubCategoryTierTwo ,如下所述:
@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
}
因此,我不想为所有这些子类别创建单独的存储库,而是要创建一个公共存储库,因此所有这些域类都实现了标记接口ISubCategory 。
我创建了一个这样的存储库:
public interface SubCategoryTierOneRepository<T extends ISubCategory> extends JpaRepository< ISubCategory, Long >
{
}
但是,当我运行我的应用程序时,出现以下错误:
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
有人可以告诉我我要去哪里了吗?
我了解这里的问题,但是您确定要执行类似的操作。 您可以重新考虑您的设计。
如果我是你,我会选择这样的东西。
@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.