Spring Data JPA - 为三个表创建@Composite 键

我从这里扩展我的问题:Define CompositeKey with three tables using JPA/Hibernate? . 在此示例中,我希望创建复合键以创建 PRODUCT_ID、CATEGORY_ID、STOCK_ID 的唯一组合。

我开发了以下代码,但不确定如何将记录保存到数据库中。

Stock.java

@Entity

public class Stock implements Serializable {


    private static final long serialVersionUID = 1L;


    @Id

    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "STOCK_ID", unique = true, nullable = false)

    private Integer stockId;


    @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)

    private String stockCode;


    @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)

    private String stockName;


    // Owner of the relationship

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "linkPk.stock", cascade = CascadeType.ALL)

    private Set<StockCategoryProductLink> stockCategoryProductLinks = new HashSet<>(0);


    public Stock() {

    }


    public Stock(Integer stockId, String stockCode, String stockName,

            Set<StockCategoryProductLink> stockCategoryProductLinks) {

        super();

        this.stockId = stockId;

        this.stockCode = stockCode;

        this.stockName = stockName;

        this.stockCategoryProductLinks = stockCategoryProductLinks;

    }


    public Integer getStockId() {

        return stockId;

    }


    public void setStockId(Integer stockId) {

        this.stockId = stockId;

    }


    public String getStockCode() {

        return stockCode;

    }


    public void setStockCode(String stockCode) {

        this.stockCode = stockCode;

    }


    public String getStockName() {

        return stockName;

    }


    public void setStockName(String stockName) {

        this.stockName = stockName;

    }


    public Set<StockCategoryProductLink> getStockCategoryProductLinks() {

        return stockCategoryProductLinks;

    }


    public void setStockCategoryProductLinks(Set<StockCategoryProductLink> stockCategoryProductLinks) {

        this.stockCategoryProductLinks = stockCategoryProductLinks;

    }

}


开满天机
浏览 81回答 1
1回答

Qyouu

好吧,我知道你说的是什么,但我认为这不是你的意思。你说实际上我有三个表,Stock、Category 和 Product。@ManyToManyStock 和 Category 之间的关系,以及@ManyToManyCategory 和 Product 之间的关系。这有助于抽象地思考这个问题。在陈记法中你说的是但是,这可能不是您的意思。这是有问题的,因为您需要Category为每个Stock和Product关系创建一个新实体。因此,如果您有一个 ETF 类别,那么它将针对 BobsBestETF 产品中的每只股票进行复制,实际上是针对每个实例化关系进行复制。你的意思可能更像是 a&nbsp;StockandProduct与Category属性的关系,就像这样。这允许许多产品,每个产品都有很多库存,每个产品/库存关系都有一个特定的类别属性。您将遇到的问题是您不想Category成为一个属性,而是一个类别查找表,如下所示:我认为这就是您要找的。使用复合 ID 实现这应该相当简单,但您显示的示例似乎有些过时或不清楚。最好找到更好的例子。这就是我为最后一个模式建模的方式。@Entitypublic class Stock {&nbsp; &nbsp; @Id @GeneratedValue(strategy = GenerationType.IDENTITY)&nbsp; &nbsp; private Long id;}@Entity@Datapublic class Product {&nbsp; &nbsp; @Id @GeneratedValue(strategy = GenerationType.IDENTITY)&nbsp; &nbsp; private Long id;}@Entitypublic class Category {&nbsp; &nbsp; @Id @GeneratedValue(strategy = GenerationType.IDENTITY)&nbsp; &nbsp; private Long id;}@Entity@Datapublic class StockProduct {&nbsp; &nbsp; @EmbeddedId&nbsp; &nbsp; private StockProductPk id;&nbsp; &nbsp; @ManyToOne&nbsp; &nbsp; @MapsId("productId")&nbsp; &nbsp; private Product product;&nbsp; &nbsp; @ManyToOne&nbsp; &nbsp; @MapsId("stockId")&nbsp; &nbsp; private Stock stock;&nbsp; &nbsp; @ManyToOne&nbsp; &nbsp; private Category category;}@Embeddable@Datapublic class StockProductPk implements Serializable {&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; private Long stockId;&nbsp; &nbsp; private Long productId;}并作为使用它的示例:private void create() {&nbsp; &nbsp; Category catEtf = new Category();&nbsp; &nbsp; categoryRepo.save(catEtf);&nbsp; &nbsp; Stock s1 = new Stock();&nbsp; &nbsp; stockRepo.save(s1);&nbsp; &nbsp; Product bobEtfs = new Product();&nbsp; &nbsp; productRepo.save(bobEtfs);&nbsp; &nbsp; // create a relationship&nbsp; &nbsp; StockProduct bs1 = new StockProduct();&nbsp; &nbsp; bs1.setId(new StockProductPk());&nbsp; &nbsp; bs1.setProduct(bobEtfs);&nbsp; &nbsp; bs1.setStock(s1);&nbsp; &nbsp; bs1.setCategory(catEtf);&nbsp; &nbsp; stockProductRepo.save(bs1);&nbsp;}private void read() {&nbsp; &nbsp; StockProduct sp1 = new StockProduct();&nbsp; &nbsp; Product p1 = new Product();&nbsp; &nbsp; p1.setId(1L);&nbsp; &nbsp; sp1.setProduct(p1);&nbsp; &nbsp; List<StockProduct> bobEtfs = stockProductRepo.findAll(Example.of(sp1, ExampleMatcher.matching()));&nbsp; &nbsp; System.out.println(bobEtfs);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java