猿问

Hibernate 生成额外的表

我有一个这样的实体


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

    }


}

我想做的是,创建一个带有名称的表,past_price它与实体有OneToMany关系。Price我有休眠属性spring.jpa.hibernate.ddl-auto=update,所以每当我运行它时,都会创建 3 个表,1. past_price 2. past_price_prices并且3. price. 但我只是想创建 2 个表past_price和price. 任何帮助,将不胜感激。谢谢


慕码人2483693
浏览 130回答 3
3回答

湖上湖

使用 @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 {&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; }}

呼啦一阵风

在javaDoc中拥有关系的字段。除非关系是单向的,否则是必需的。使用 targetEntity=price.class&nbsp;class pastPrice{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@OneToMany(targetEntity=Price.class,fetch = FetchType.EAGER,cascade = CascadeType.ALL)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;private Set<Price> prices = null;&nbsp; &nbsp; }或使用 mappedby=priceclass pastPrice{&nbsp; &nbsp; &nbsp;@OneToMany(mappedby="Price",fetch = FetchType.EAGER,cascade = CascadeType.ALL)&nbsp; &nbsp; &nbsp; private Set<Price> prices = null;}&nbsp; &nbsp;@Entity("Price)&nbsp; &nbsp; class Price{}

GCT1015

事实上,问题在于您没有为实体提供其他映射。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
随时随地看视频慕课网APP

相关分类

Java
我要回答