org.hibernate.MappingException: Repeated column in mapping for entity. Hibernate Mapping Exception

陌路散爱 提交于 2019-12-11 19:35:02

问题


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;
}
}

And

@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;
}
}

When I run this code:

Session session = getCoreObject().getDataBase().getCoreSession();
AccountsEntity fieldsEntity = (AccountsEntity) session.load(AccountsEntity.class,1);

I have exception:

org.hibernate.MappingException: Repeated column in mapping for entity: com.company.hrplus.core.rest.Account.enteties.AccountsDetailDataEntity column: id (should be mapped with insert="false" update="false")

I can't understant where is the problem. Please help.


回答1:


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")



回答2:


I guess u have 2 columns called ID; ons is the PK column of entity AccountsDetailDataEntity coinciding with the declaration:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

the other one is the joincolumn; try to change name to the joincolumn or to the id column

I hope this can help

Angelo



来源:https://stackoverflow.com/questions/23458076/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!