有人可以解释一下「实体的映射中存在重复的列」是什么意思吗?

huangapple go评论75阅读模式
英文:

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中移除datesymbol。请注意,这不是将两个表连接起来,因为不会有一个单独的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.

huangapple
  • 本文由 发表于 2020年9月15日 10:54:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63894391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定