2

I'm using generation tools to generate Hibernate entity, and I get a error like the following:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: model.PayLogEntity column: brank_card_id (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 67 more

And the class that throws error is as following:

package model;

import javax.persistence.*;
import java.sql.Timestamp;

/**
 * Created by shenshijun on 14-6-5.
 */
@Entity
@Table(name = "pay_log", schema = "", catalog = "healthclube")
public class PayLogEntity {
    private int id;
    private Integer userId;
    private String brankCardId;
    private String payType;
    private Timestamp payTime;
    private Double count;
    private String message;
    private BankCardEntity bankCardByBrankCardId;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "user_id")
    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Basic
    @Column(name = "brank_card_id")
    public String getBrankCardId() {
        return brankCardId;
    }

    public void setBrankCardId(String brankCardId) {
        this.brankCardId = brankCardId;
    }

    @Basic
    @Column(name = "pay_type")
    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    @Basic
    @Column(name = "pay_time")
    public Timestamp getPayTime() {
        return payTime;
    }

    public void setPayTime(Timestamp payTime) {
        this.payTime = payTime;
    }

    @Basic
    @Column(name = "count")
    public Double getCount() {
        return count;
    }

    public void setCount(Double count) {
        this.count = count;
    }

    @Basic
    @Column(name = "message")
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PayLogEntity that = (PayLogEntity) o;

        if (id != that.id) return false;
        if (brankCardId != null ? !brankCardId.equals(that.brankCardId) : that.brankCardId != null) return false;
        if (count != null ? !count.equals(that.count) : that.count != null) return false;
        if (message != null ? !message.equals(that.message) : that.message != null) return false;
        if (payTime != null ? !payTime.equals(that.payTime) : that.payTime != null) return false;
        if (payType != null ? !payType.equals(that.payType) : that.payType != null) return false;
        if (userId != null ? !userId.equals(that.userId) : that.userId != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (userId != null ? userId.hashCode() : 0);
        result = 31 * result + (brankCardId != null ? brankCardId.hashCode() : 0);
        result = 31 * result + (payType != null ? payType.hashCode() : 0);
        result = 31 * result + (payTime != null ? payTime.hashCode() : 0);
        result = 31 * result + (count != null ? count.hashCode() : 0);
        result = 31 * result + (message != null ? message.hashCode() : 0);
        return result;
    }

    @ManyToOne
    @JoinColumn(name = "brank_card_id", referencedColumnName = "id")
    public BankCardEntity getBankCardByBrankCardId() {
        return bankCardByBrankCardId;
    }

    public void setBankCardByBrankCardId(BankCardEntity bankCardByBrankCardId) {
        this.bankCardByBrankCardId = bankCardByBrankCardId;
    }
}


It seems that brank_card_id in the entity had beaning mapping twice, one is getBrankCardId and the other is getBankCardByBrankCardId. So my question is how can I change the code to remove the conflict?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ssj
  • 1,737
  • 2
  • 16
  • 28
  • actually I have find a similar question [link](http://stackoverflow.com/questions/4892925/how-can-i-map-insert-false-update-false-on-a-composite-id-key-property-whi) which will help. – ssj Jun 05 '14 at 02:57

1 Answers1

0

What Hibernate is saying is that you have 2 column defined with the name brank_card_id in this table. One is:

@Basic
@Column(name = "brank_card_id")
public String getBrankCardId() {
    return brankCardId;
}

and the other is:

@ManyToOne
@JoinColumn(name = "brank_card_id", referencedColumnName = "id")
public BankCardEntity getBankCardByBrankCardId() {
    return bankCardByBrankCardId;
}

The name attribute of the @JoinColumn annotation is the actual name of the foreign key column.

You can't have 2 columns called brank_card_id. Instead of calling your join column brank_card_id call it something else, like bank_card_fk. Anything should work. just make sure you reference it correctly on the other side of the relationship.

Change the @JoinColumn annotation to match the following:

@ManyToOne
@JoinColumn(name = "bank_card_fk", referencedColumnName = "id")
public BankCardEntity getBankCardByBrankCardId() {
    return bankCardByBrankCardId;
}
JamesENL
  • 6,400
  • 6
  • 39
  • 64
  • `@JoinColumn(name = "brank_card_id", referencedColumnName = "id")`. you can't change name to `brank_card_id` to `brank_card_pk` because `brank_card_id` is mapping to a column in table and `brank_card_pk` even do not exist in table. – ssj Jun 05 '14 at 02:44
  • I got your idea, but I do not think it a very good idea. Because the change you suggested makes the mapping of this side useless and in the other side I have : `@OneToMany(mappedBy = "userByUserId") public Collection getBankCardsById() { return bankCardsById; }`. So the changes will make mapping fail – ssj Jun 05 '14 at 02:55
  • But your `@OneToMany` annotation above won't even pointing at the `PayLogEntity` obejct and corresponding table. It will be pointing at the `BankCardEntity` object and table. – JamesENL Jun 05 '14 at 03:12