Hibernate生成额外的表

时间:2019-03-11 05:43:06

标签: java hibernate spring-boot spring-mvc spring-data-jpa

我有一个这样的实体

@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_prices3. price。但是我仅尝试创建2个表past_priceprice。任何帮助,将不胜感激。谢谢

4 个答案:

答案 0 :(得分:1)

使用@JoinColumn告诉Hibernate在价格表中创建一列并将其用于联接。将您的代码更改为以下代码:

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

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

}

答案 1 :(得分:0)

实际上,您没有为实体提供其他映射的问题。休眠状态将如何识别与Price相关的PastPrice? Hibernate无法(但不确定)将相关ID数组存储在Price中。

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

@ManyToOne(...)
private PastPrice pastPrice;

在这种情况下,表Price休眠会生成ID为“父级”价格的colymn。因此,对于当前的映射,两个表就足够了。

工作方式如下:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您应该添加“ mappedBy”以声明哪个字段是关联表。

还要在Price实体中添加ManyToOne。

价格实体:

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn("past_price_fk")
private PastPrice pastPrice;

PastPrice实体:

@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL, mappedBy = "pastPrice")
private Set<Price> prices = null;