本文介绍了org.hibernate.MappingException:实体映射中的重复列.休眠映射异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个表:Accounts和AccountDetailsData.我将其映射为2个类:
I have 2 tables: Accounts and AccountDetailsData. I mapped there are to 2 classes:
@Entity
@Table(name = "Accounts", schema = "dbo", catalog = "core")
public class AccountsEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Basic
@Column(name = "status")
private String status;
@Basic
@Column(name = "enabled")
private Boolean enabled;
@Basic
@Column(name = "timestamp")
private Timestamp timestamp;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="accountsEntityObject")
private List<AccountsDetailDataEntity> detailDataEntity;
public AccountsEntity() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountsEntity that = (AccountsEntity) o;
if (enabled != null ? !enabled.equals(that.enabled) : that.enabled != null) return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (status != null ? !status.equals(that.status) : that.status != null) return false;
if (timestamp != null ? !timestamp.equals(that.timestamp) : that.timestamp != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (status != null ? status.hashCode() : 0);
result = 31 * result + (enabled != null ? enabled.hashCode() : 0);
result = 31 * result + (timestamp != null ? timestamp.hashCode() : 0);
return result;
}
}
和
@Entity
@Table(name = "AccountsDetailData", schema = "dbo", catalog = "core")
public class AccountsDetailDataEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Basic
@Column(name = "additionalValue")
private String additionalValue;
@Basic
@Column(name = "status")
private String status;
@Basic
@Column(name = "enabled")
private Boolean enabled;
@ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
@JoinColumn(name = "id")
private AccountsEntity accountsEntityObject;
public AccountsDetailDataEntity() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAdditionalValue() {
return additionalValue;
}
public void setAdditionalValue(String additionalValue) {
this.additionalValue = additionalValue;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountsDetailDataEntity that = (AccountsDetailDataEntity) o;
if (additionalValue != null ? !additionalValue.equals(that.additionalValue) : that.additionalValue != null)
return false;
if (enabled != null ? !enabled.equals(that.enabled) : that.enabled != null) return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (status != null ? !status.equals(that.status) : that.status != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (additionalValue != null ? additionalValue.hashCode() : 0);
result = 31 * result + (status != null ? status.hashCode() : 0);
result = 31 * result + (enabled != null ? enabled.hashCode() : 0);
return result;
}
}
当我运行此代码时:
Session session = getCoreObject().getDataBase().getCoreSession();
AccountsEntity fieldsEntity = (AccountsEntity) session.load(AccountsEntity.class,1);
我有例外:
我不能弄清楚问题出在哪里.请帮忙.
I can't understant where is the problem. Please help.
推荐答案
stacktrace建议不要保留id
列,但实际上,您只需要更改连接列名,以使其不再与主键冲突
The stacktrace is suggesting that the id
column not be persisted but realistically you just need to change the join column name so it no longer conflicts with the primary key
@JoinColumn(name = "accounts_entity_id")
这篇关于org.hibernate.MappingException:实体映射中的重复列.休眠映射异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!