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 this
@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. 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
. But I am only trying to create 2 tables past_price
and price
. Any help would be appreciated. Thanks
Use @JoinColumn to tell hibernate to create a column in price table and use that for joining. 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.
P.S.: Use bidirectional association instead if there's no strong reason to go with uni-directional. 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;
}
}