外键和引用类如何在Hibernate实体中共存?

时间:2014-06-05 02:34:57

标签: hibernate jpa

我正在使用生成工具来生成Hibernate实体,我收到如下错误:

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

抛出错误的类如下:

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


实体中的brank_card_id似乎有两次beaning映射,一个是getBrankCardId,另一个是getBankCardByBrankCardId。所以我的问题是如何更改代码以消除冲突?

1 个答案:

答案 0 :(得分:0)

Hibernate所说的是你在这个表中定义了名为brank_card_id的2列。一个是:

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

另一个是:

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

@JoinColumn注释的name属性是外键列的实际名称。

您不能拥有名为brank_card_id的2列。而不是调用您的加入列brank_card_id,而是将其称为其他内容,例如bank_card_fk。什么都行不通。只要确保你在关系的另一面正确引用它。

更改@JoinColumn注释以匹配以下内容:

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