英文:
Can someone please explain "Repeated column in mapping for entity"?
问题
我一直在收到错误:
> org.hibernate.MappingException: 实体的映射中有重复的列:
> net.tekknow.moneymachine.model.Quote 列:date(应该使用 insert="false" update="false" 进行映射)
无论我尝试什么。
这是我的 Quote 类:
public class Quote {
@EmbeddedId
private QuoteId quoteId; //hibernate 需要的复合 ID
private String symbol;
private Date date;
private float close;
public Quote() {}
public Quote(String symbol, Date date, float close) {
this.symbol = symbol;
this.date = date;
this.close = close;
}
public QuoteId getId() {
return quoteId;
}
public void setId(QuoteId id) {
this.quoteId = id;
}
@Column(name = "symbol", insertable = false, updatable = false, nullable = false)
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
@Column(name = "date", insertable = false, updatable = false, nullable = false)
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
... //其他的 getter 和 setter 方法
这是 QuoteId 类,根据 https://vladmihalcea.com/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/ 所述,这个类应该由 Hibernate 需要来将两列作为复合主键进行连接:
@Embeddable
public class QuoteId implements Serializable {
@Column(name = "symbol", insertable = false, updatable = false)
private String symbol;
@Column(name = "date", insertable = false, updatable = false)
private Date date;
public QuoteId(String symbol, Date date) {
this.symbol = symbol;
this.date = date;
}
public QuoteId() {}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
... //其他的 getter 和 setter 方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
QuoteId that = (QuoteId) o;
return Objects.equals(symbol, that.symbol) && Objects.equals(date, that.date);
}
@Override
public int hashCode() {
return Objects.hash(symbol, date);
}
}
希望您这些专家能告诉我我在做什么错误的地方。
英文:
I keep getting error:
> org.hibernate.MappingException: Repeated column in mapping for entity:
> net.tekknow.moneymachine.model.Quote column: date (should be mapped
> with insert="false" update="false")"
no matter what I try.
Here is my Quote class:
public class Quote {
@EmbeddedId
private QuoteId quoteId; //composite id needed by hibernate
private String symbol;
private Date date;
private float close;
public Quote() {}
public Quote(String symbol, Date date, float close) {
this.symbol = symbol;
this.date = date;
this.close = close;
}
public QuoteId getId() {
return quoteId;
}
public void setId(QuoteId id) {
this.quoteId = id;
}
@Column(name = "symbol", insertable = false, updatable = false, nullable = false)
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
@Column(name = "date", insertable = false, updatable = false, nullable = false)
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
... //other getters and setters
Here is QuoteId class which is supposed to be needed by Hibernate to join two columns as a composite key per https://vladmihalcea.com/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/
@Embeddable
public class QuoteId implements Serializable {
@Column(name = "symbol", insertable = false, updatable = false)
private String symbol;
@Column(name = "date", insertable = false, updatable = false)
private Date date;
public QuoteId(String symbol, Date date) {
this.symbol = symbol;
this.date = date;
}
public QuoteId() {}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
... // other getters and setters
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
QuoteId that = (QuoteId) o;
return Objects.equals(symbol, that.symbol) && Objects.equals(date, that.date);
}
@Override
public int hashCode() {
return Objects.hash(symbol, date);
}
}
Hoping you experts can tell me what I'm doing wrong.
答案1
得分: 2
你的QuoteId
类被标记为@Embeddable
,这意味着用于Quote
的数据库表会拉取QuoteId
中的所有列,除了它自己的列。
由于你在两个表中都有一个名为date
的列(顺便提一下,symbol
也是),最终你会得到一个具有相同列名的表定义,这是不允许的。
如果你想为Quote
使用复合主键,你需要从Quote
中移除date
和symbol
。请注意,这不是将两个表连接起来,因为不会有一个单独的QuoteId
表。
英文:
Your QuoteId
class is marked as @Embeddable
which means that a database table for Quote
pulls in all columns from QuoteId
in addition to its own columns.
Becuse you have a column with name date
(and, by the way, symbol
too) in both tables, you end up with a table definition which has two columns with the same name, which is not allowed.
If you are trying to use a composite key for Quote
, you need to remove date
and symbol
from Quote
. Note that this is not joining two tables, as there will be no QuoteId
as a separate table.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论