在我們當前的項目中,我們遇到了一些困難。我最近更改了一些Hibernate Bean(我們的Article Bean和一些底層的東西),然後我運行了一些測試,一切都很正常。現在我的隊友是有例外此消息:Hibernate的外鍵問題(Bug?)
Foreign key (FK_09fd525ae6654c059394d22cc15:ARTBILDER [artikel_fk,servdat_fk])) must have same number of columns as the referenced primary key (ARTIKEL [AUTOIN_FIX])
的註解是絕對正確的。我遇到了同樣的問題,並決定從頭開始在我的計算機上安裝該項目,問題消失了。什麼可能是這些問題的原因?
我們正在研究一個遺留數據庫,並且只將我們的對象映射到數據庫,而不是用休眠生成數據庫。我們使用HibernateSearch進行全文搜索(也許這是相關的,因爲第一次發生這種情況是在添加了HibernateSearch註解之後)。
我們正在使用Firebird 2.5實例。
編輯:
這裏的錯誤是來自屬性:
的ID類:
@Embeddable
public class ID implements Serializable {
private static final long serialVersionUID = 1810044174631580834L;
private Long autoin;
private Integer serverId;
public ID() {
}
public ID(Long autoin, Integer serverId) {
this.autoin = autoin;
this.serverId = serverId;
}
@Column(name = "autoin_fix")
public Long getAutoin() {
return this.autoin;
}
@Column(name = "servdat_fk")
public Integer getServerId() {
return this.serverId;
}
public void setAutoin(Long autoin) {
this.autoin = autoin;
}
public void setServerId(Integer serverId) {
this.serverId = serverId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((this.autoin == null) ? 0 : this.autoin.hashCode());
result = prime * result
+ ((this.serverId == null) ? 0 : this.serverId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
ID other = (ID) obj;
if (this.autoin == null) {
if (other.autoin != null) {
return false;
}
} else if (!this.autoin.equals(other.autoin)) {
return false;
}
if (this.serverId == null) {
if (other.serverId != null) {
return false;
}
} else if (!this.serverId.equals(other.serverId)) {
return false;
}
return true;
}
@Override
public String toString() {
return new StringBuilder().append("ID [").append(this.autoin)
.append("_").append(this.serverId).append("]").toString();
}
}
物件類別:
@Indexed
@Entity
@Table(name = "ARTIKEL")
public class Article {
private ID id;
private List<Picture> pictures;
...
@DocumentId
@EmbeddedId
@FieldBridge(impl = IDBridge.class)
public ID getId() {
return id;
}
@OneToMany
@JoinColumns({
@JoinColumn(name = "artikel_fk", referencedColumnName = "autoin_fix"),
@JoinColumn(name = "servdat_fk", referencedColumnName = "servdat_fk") })
@IndexedEmbedded
public List<Picture> getPictures() {
return pictures;
}
}
圖片類:
@Entity
@Table(name = "ARTBILDER")
public class Picture extends BasePicture {
...
protected ID id;
@EmbeddedId
@FieldBridge(impl = IDBridge.class)
@Field(store = Store.YES, index = Index.YES)
public ID getId() {
return id;
}
...
}
編輯2:我可能有一個線索,這從哪裏來,請待機。編輯3:不,不是錯誤。
EDIT4:這裏是DDL:
CREATE TABLE ARTIKEL
(
AUTOIN_FIX NUM10_0 DEFAULT 0,
SERVDAT_FK NUM10_0 DEFAULT 0,
...
PRIMARY KEY (AUTOIN_FIX,SERVDAT_FK)
);
CREATE TABLE ARTBILDER
(
AUTOIN_FIX NUM10_0 DEFAULT 0,
ARTIKEL_FK NUM10_0 DEFAULT 0,
SERVDAT_FK NUM10_0 DEFAULT 0,
...
PRIMARY KEY (AUTOIN_FIX,SERVDAT_FK)
);
您可以發佈完整的堆棧跟蹤? –
錯誤很明顯。在一個表中有一個圓柱形主鍵,而在另一個表中有一個組合外鍵。您無法創建連接這兩個鍵的關係。你的情況很奇怪。你能提供更多信息嗎?實體,數據庫結構和堆棧跟蹤。 –
full stacktrace不可用,sry。 –