简体   繁体   English

Hibernate 生成额外的表

[英]Hibernate generates extra table

I have an Entity like this我有一个这样的实体

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

}

And the Price entity is like thisPrice实体是这样的

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

}

What I am trying to do is, create a table with name past_price and it has OneToMany relationship with Price entity.我想要做的是,创建一个名为past_price的表,它与Price实体具有OneToMany关系。 I have hibernate property spring.jpa.hibernate.ddl-auto=update so whenever I run this there are 3 tables created 1. past_price 2. past_price_prices and 3. price .我有休眠属性spring.jpa.hibernate.ddl-auto=update所以每当我运行它时都会创建 3 个表1. past_price 2. past_price_prices3. price But I am only trying to create 2 tables past_price and price .但我只是想创建 2 个表past_priceprice Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

Use @JoinColumn to tell hibernate to create a column in price table and use that for joining.使用 @JoinColumn 告诉 hibernate 在价格表中创建一列并将其用于加入。 Change your code to below:将您的代码更改为以下内容:

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

This will create a column named fk_past_price in price table and no third table will be created.这将在价格表中创建一个名为 fk_past_price 的列,并且不会创建第三个表。

PS: Use bidirectional association instead if there's no strong reason to go with uni-directional. PS:如果没有充分的理由使用单向,请改用双向关联。 Like below:像下面这样:

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

}

Price:价钱:

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

}

Problem in fact, that you didn't provide other mapping for entities.事实上,问题是您没有为实体提供其他映射。 How would hibernate identify that Price related to PastPrice ?如何将休眠确定Price有关PastPrice Hibernate couldn't (but I'm not sure) store array of related ids in Price . Hibernate 不能(但我不确定)在Price存储相关 id 的数组。

By default, if you want just 2 tables you need to add field in Price :默认情况下,如果您只需要 2 个表,则需要在Price添加字段:

@ManyToOne(...)
private PastPrice pastPrice;

In this case, in table Price hibernate generate colymn with id of 'parent' price.在这种情况下,在表 Price hibernate 中生成带有 'parent' price id 的 colymn。 Thus, for current mapping 2 tables would be enough.因此,对于当前的映射,2 个表就足够了。

It would be work something like:它会像这样工作:

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

in javaDocjavaDoc 中

The field that owns the relationship.拥有关系的字段。 Required unless the relationship is unidirectional.除非关系是单向的,否则是必需的。

using targetEntity=price.class使用 targetEntity=price.class

 class pastPrice{
         @OneToMany(targetEntity=Price.class,fetch = FetchType.EAGER,cascade = CascadeType.ALL)
         private Set<Price> prices = null;
    }

or using mappedby=price或使用mappedby=price

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

   @Entity("Price)
    class Price{
}

You should add "mappedBy" to declare which field is relate tables.您应该添加“mappedBy”来声明哪个字段是关联表。

also add ManyToOne in Price entity.还在价格实体中添加 ManyToOne。

Price Entity :价格实体:

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

PastPrice Entity :过去价格实体:

@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