gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:18:39 24 4
gpt4 key购买 nike

我有两个域类,即 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
}

因此,我不想为所有这些子类别创建单独的存储库,而是想创建一个公共(public)存储库,因此所有这些域类都实现标记接口(interface) 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<>();
}

关于java - Spring Data JPA 如何为实现公共(public)接口(interface)的两个域类创建单个存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57287860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com