休眠生成额外的表

我有一个这样的实体


@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


@Entity

public 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;

    }


}

我试图做的是,创建一个带有名称的表,它与实体有关系。我有休眠属性,所以每当我运行这个时,都会创建3个表和.但我只是试图创建2个表和.任何帮助将不胜感激。谢谢past_priceOneToManyPricespring.jpa.hibernate.ddl-auto=update1. past_price2. past_price_prices3. pricepast_priceprice


婷婷同学_
浏览 106回答 3
3回答

12345678_0001

使用@JoinColumn告诉休眠在价格表中创建列,并将其用于联接。将您的代码更改为以下内容:@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)@JoinColumn(name = "fk_past_price")private Set<Price> prices = null;这将在价格表中创建一个名为 fk_past_price 的列,并且不会创建第三个表。P.S.:如果没有充分的理由使用单向关联,请使用双向关联。如下图所示:@Entity@Table(name = "past_price")public class PastPrice {&nbsp; &nbsp; @Id&nbsp; &nbsp; private String symbol;&nbsp; &nbsp; @OneToMany(mappedBy = "pastPrice", cascade = CascadeType.ALL, fetch = FetchType.EAGER)&nbsp; &nbsp; private Set<Price> prices = null;&nbsp; &nbsp; public String getSymbol() {&nbsp; &nbsp; &nbsp; &nbsp; return symbol;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSymbol(String symbol) {&nbsp; &nbsp; &nbsp; &nbsp; this.symbol = symbol;&nbsp; &nbsp; }&nbsp; &nbsp; public Set<Price> getPrices() {&nbsp; &nbsp; &nbsp; &nbsp; return prices;&nbsp; &nbsp; }&nbsp; &nbsp; public void setPrices(Set<Price> prices) {&nbsp; &nbsp; &nbsp; &nbsp; this.prices = prices;&nbsp; &nbsp; }}价格:@Entitypublic class Price {&nbsp; &nbsp; @Id&nbsp; &nbsp; @Temporal(TemporalType.TIMESTAMP)&nbsp; &nbsp; private Date date;&nbsp; &nbsp; private String price;&nbsp; &nbsp; @ManyToOne&nbsp; &nbsp; @JoinColumn(name = "past_price_symbol", nullable = false)&nbsp; &nbsp; private PastPrice pastPrice;&nbsp; &nbsp; public PastPrice getPastPrice() {&nbsp; &nbsp; &nbsp; return pastPrice;&nbsp; &nbsp; }&nbsp; &nbsp; public void setPastPrice(PastPrice pastPrice) {&nbsp; &nbsp; &nbsp; this.pastPrice = pastPrice;&nbsp; &nbsp; }&nbsp; &nbsp; public Date getDate() {&nbsp; &nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; &nbsp; public void setDate(Date date) {&nbsp; &nbsp; &nbsp; &nbsp; this.date = date;&nbsp; &nbsp; }&nbsp; &nbsp; public String getPrice() {&nbsp; &nbsp; &nbsp; &nbsp; return price;&nbsp; &nbsp; }&nbsp; &nbsp; public void setPrice(String price) {&nbsp; &nbsp; &nbsp; &nbsp; this.price = price;&nbsp; &nbsp; }}

倚天杖

您应该添加“mappedBy”来声明哪个字段是相关表。还在价格实体中添加 ManyToOne。价格实体 :@ManyToOne(fetch = FetchType.EAGER)@JoinColumn("past_price_fk")private PastPrice pastPrice;往年价格实体 :@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL, mappedBy = "pastPrice")private Set<Price> prices = null;

偶然的你

实际上,问题是您没有为实体提供其他映射。休眠如何识别与 ?休眠无法(但我不确定)在 中存储相关ID的数组。PricePastPricePrice默认情况下,如果您只需要2个表,则需要在中添加字段:Price@ManyToOne(...)private PastPrice pastPrice;在本例中,在“价格休眠”表中,生成 id 为“父”价格的 colymn。因此,对于当前映射,2个表就足够了。这将是这样的工作:select * from Price p join PastPrice pp on pp.id = p.past_price
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java