第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Spring Data JPA - 為三個(gè)表創(chuàng)建@Composite 鍵

Spring Data JPA - 為三個(gè)表創(chuàng)建@Composite 鍵

開滿天機(jī) 2023-05-10 13:25:47
我從這里擴(kuò)展我的問(wèn)題:Define CompositeKey with three tables using JPA/Hibernate? . 在此示例中,我希望創(chuàng)建復(fù)合鍵以創(chuàng)建 PRODUCT_ID、CATEGORY_ID、STOCK_ID 的唯一組合。我開發(fā)了以下代碼,但不確定如何將記錄保存到數(shù)據(jù)庫(kù)中。Stock.java@Entitypublic 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;    }}
查看完整描述

1 回答

?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

好吧,我知道你說(shuō)的是什么,但我認(rèn)為這不是你的意思。你說(shuō)

實(shí)際上我有三個(gè)表,Stock、Category 和 Product。@ManyToManyStock 和 Category 之間的關(guān)系,以及@ManyToManyCategory 和 Product 之間的關(guān)系。

這有助于抽象地思考這個(gè)問(wèn)題。在陳記法中你說(shuō)的是

http://img1.sycdn.imooc.com//645b2afd000138fa05670062.jpg

但是,這可能不是您的意思。這是有問(wèn)題的,因?yàn)槟枰?code>Category為每個(gè)StockProduct關(guān)系創(chuàng)建一個(gè)新實(shí)體。因此,如果您有一個(gè) ETF 類別,那么它將針對(duì) BobsBestETF 產(chǎn)品中的每只股票進(jìn)行復(fù)制,實(shí)際上是針對(duì)每個(gè)實(shí)例化關(guān)系進(jìn)行復(fù)制。

你的意思可能更像是 a StockandProductCategory屬性的關(guān)系,就像這樣。

http://img1.sycdn.imooc.com//645b2b090001aec203610142.jpg

這允許許多產(chǎn)品,每個(gè)產(chǎn)品都有很多庫(kù)存,每個(gè)產(chǎn)品/庫(kù)存關(guān)系都有一個(gè)特定的類別屬性。您將遇到的問(wèn)題是您不想Category成為一個(gè)屬性,而是一個(gè)類別查找表,如下所示:

http://img1.sycdn.imooc.com//645b2b180001c7ff03600162.jpg

我認(rèn)為這就是您要找的。使用復(fù)合 ID 實(shí)現(xiàn)這應(yīng)該相當(dāng)簡(jiǎn)單,但您顯示的示例似乎有些過(guò)時(shí)或不清楚。最好找到更好的例子。這就是我為最后一個(gè)模式建模的方式。


@Entity

public class Stock {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

}

@Entity

@Data

public class Product {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

}

@Entity

public class Category {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

}

@Entity

@Data

public class StockProduct {

    @EmbeddedId

    private StockProductPk id;


    @ManyToOne

    @MapsId("productId")

    private Product product;

    @ManyToOne

    @MapsId("stockId")

    private Stock stock;


    @ManyToOne

    private Category category;

}

@Embeddable

@Data

public class StockProductPk implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long stockId;

    private Long productId;

}

并作為使用它的示例:


private void create() {

    Category catEtf = new Category();

    categoryRepo.save(catEtf);

    Stock s1 = new Stock();

    stockRepo.save(s1);

    Product bobEtfs = new Product();

    productRepo.save(bobEtfs);


    // create a relationship

    StockProduct bs1 = new StockProduct();

    bs1.setId(new StockProductPk());

    bs1.setProduct(bobEtfs);

    bs1.setStock(s1);

    bs1.setCategory(catEtf);

    stockProductRepo.save(bs1); 

}

private void read() {

    StockProduct sp1 = new StockProduct();

    Product p1 = new Product();

    p1.setId(1L);

    sp1.setProduct(p1);

    List<StockProduct> bobEtfs = stockProductRepo.findAll(Example.of(sp1, ExampleMatcher.matching()));

    System.out.println(bobEtfs);

}


查看完整回答
反對(duì) 回復(fù) 2023-05-10
  • 1 回答
  • 0 關(guān)注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)