spring-data-jpa 如何为具有多对多Map的表创建JPA存储库

uxhixvfz  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(178)

我创建了以下java类

产品.java

@Entity
public class Product {

    @Id
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @GeneratedValue(generator = "UUID")
    private UUID id;
    private String name;

    @Lob
    private String description;
    private String category;
    private String itemCode;

    @OneToMany(mappedBy = "product")
    private List<ProductFeatures> productFeatures;

    @ManyToMany
    @JoinTable(
            name = "product_tags",
            joinColumns = @JoinColumn(name = "product_id"),
            inverseJoinColumns = @JoinColumn(name = "tag_id")
    )
    private List<Tag> productTags;

    private int quantity;
    private Timestamp dateModified;

    @ManyToOne
    @JoinColumn(name = "vendor_id")
    private Vendor vendor;

}

标记.java

@Entity
public class Tag {

    @Id
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @GeneratedValue(generator = "UUID")
    private UUID id;
    private String name;

    @Lob
    private String description;

    @ManyToMany(mappedBy = "productTags")
    private List<Product> products;

}

这两个类将创建三个表 productstagsproduct_tags。现在,我想为product_tags表创建一个存储库。我该怎么做呢?

如有任何建议,我们将不胜感激。

b91juud3

b91juud31#

您不能。JpaRepository需要实体,但没有ProductTag实体。product_tag只是一个连接表,用于满足Product和Tag之间的ManyToMany关系。您需要的JpaReposories是Product和Tag

@Repository
public interface ProductRepository extends JpaRepository<Product, UUID> {
}

@Repository
public interface TagRepository extends JpaRepository<Tag, UUID> {
}

相关问题