我有2个表:Accounts和AccountDetailsData。我映射了两个类:
@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);
我有例外:
org.hibernate.MappingException:实体映射中的重复列:com.company.hrplus.core.rest.Account.enteties.AccountsDetailDataEntity column:id(应使用insert =&#34; false&#34; update进行映射) =&#34;假&#34)
我无法理解问题出在哪里。请帮忙。
答案 0 :(得分:0)
堆栈跟踪建议不要保留id
列,但实际上您只需更改连接列名称,使其不再与主键冲突
@JoinColumn(name = "accounts_entity_id")
答案 1 :(得分:0)
我猜你有两列名为ID; ons是实体AccountsDetailDataEntity的PK列,与声明一致:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
另一个是连接列;尝试将名称更改为连接列或id列
我希望这可以提供帮助
安吉洛