亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Hibernate 生成額外的表

Hibernate 生成額外的表

慕碼人2483693 2022-07-27 21:58:54
我有一個這樣的實體@Entity@Table(name = "past_price")public class PastPrice {    @Id    private String symbol;    @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)    private Set<Price> prices = null;    public String getSymbol() {        return symbol;    }    public void setSymbol(String symbol) {        this.symbol = symbol;    }    public Set<Price> getPrices() {        return prices;    }    public void setPrices(Set<Price> prices) {        this.prices = prices;    }}而Price實體是這樣的@Entitypublic class Price {    @Id    @Temporal(TemporalType.TIMESTAMP)    private Date date;    private String price;    public Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }    public String getPrice() {        return price;    }    public void setPrice(String price) {        this.price = price;    }}我想做的是,創建一個帶有名稱的表,past_price它與實體有OneToMany關系。Price我有休眠屬性spring.jpa.hibernate.ddl-auto=update,所以每當我運行它時,都會創建 3 個表,1. past_price 2. past_price_prices并且3. price. 但我只是想創建 2 個表past_price和price. 任何幫助,將不勝感激。謝謝
查看完整描述

3 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

使用 @JoinColumn 告訴 hibernate 在價格表中創建一個列并將其用于連接。將您的代碼更改為以下內容:


@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)

@JoinColumn(name = "fk_past_price")

private Set<Price> prices = null;

這將在價格表中創建一個名為 fk_past_price 的列,并且不會創建第三個表。


PS:如果沒有充分的理由使用單向關聯,請改用雙向關聯。如下所示:


@Entity

@Table(name = "past_price")

public class PastPrice {


    @Id

    private String symbol;

    @OneToMany(mappedBy = "pastPrice", cascade = CascadeType.ALL, fetch = FetchType.EAGER)

    private Set<Price> prices = null;


    public String getSymbol() {

        return symbol;

    }


    public void setSymbol(String symbol) {

        this.symbol = symbol;

    }


    public Set<Price> getPrices() {

        return prices;

    }


    public void setPrices(Set<Price> prices) {

        this.prices = prices;

    }


}

價格:


@Entity

public class Price {

    @Id

    @Temporal(TemporalType.TIMESTAMP)

    private Date date;

    private String price;


    @ManyToOne

    @JoinColumn(name = "past_price_symbol", nullable = false)

    private PastPrice pastPrice;


    public PastPrice getPastPrice() {

      return pastPrice;

    }


    public void setPastPrice(PastPrice pastPrice) {

      this.pastPrice = pastPrice;

    }


    public Date getDate() {

        return date;

    }


    public void setDate(Date date) {

        this.date = date;

    }


    public String getPrice() {

        return price;

    }


    public void setPrice(String price) {

        this.price = price;

    }


}


查看完整回答
反對 回復 2022-07-27
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

在javaDoc中


擁有關系的字段。除非關系是單向的,否則是必需的。


使用 targetEntity=price.class


 class pastPrice{

         @OneToMany(targetEntity=Price.class,fetch = FetchType.EAGER,cascade = CascadeType.ALL)

         private Set<Price> prices = null;

    }

或使用 mappedby=price


class pastPrice{

     @OneToMany(mappedby="Price",fetch = FetchType.EAGER,cascade = CascadeType.ALL)

      private Set<Price> prices = null;

}


   @Entity("Price)

    class Price{

}



查看完整回答
反對 回復 2022-07-27
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

事實上,問題在于您沒有為實體提供其他映射。hibernate 如何識別Price與 相關的PastPrice?Hibernate 不能(但我不確定)在Price.


默認情況下,如果您只需要 2 個表,則需要在以下位置添加字段Price:


@ManyToOne(...)

private PastPrice pastPrice;

在這種情況下,在表 Price hibernate 中生成 id 為“父”價格的 colymn。因此,對于當前映射 2 個表就足夠了。


這將是這樣的:


select * from Price p join PastPrice pp on pp.id = p.past_price


查看完整回答
反對 回復 2022-07-27
  • 3 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號