繁体   English   中英

Hibernate 生成额外的表

[英]Hibernate generates extra table

我有一个这样的实体

@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的表,它与Price实体具有OneToMany关系。 我有休眠属性spring.jpa.hibernate.ddl-auto=update所以每当我运行它时都会创建 3 个表1. past_price 2. past_price_prices3. price 但我只是想创建 2 个表past_priceprice 任何帮助,将不胜感激。 谢谢

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

}

事实上,问题是您没有为实体提供其他映射。 如何将休眠确定Price有关PastPrice Hibernate 不能(但我不确定)在Price存储相关 id 的数组。

默认情况下,如果您只需要 2 个表,则需要在Price添加字段:

@ManyToOne(...)
private PastPrice pastPrice;

在这种情况下,在表 Price hibernate 中生成带有 'parent' price id 的 colymn。 因此,对于当前的映射,2 个表就足够了。

它会像这样工作:

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

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{
}

您应该添加“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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM