Hibernate中的ManyToOne多个连接列

时间:2015-12-07 06:08:37

标签: java mysql spring hibernate

我有2张桌子, 一个是codes_category,另一个是代码。 我在代码表中创建了一个两个连接列,两列是code_category表中的主键。下面是代码表的实体。

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * IcdCodes generated by hbm2java
 */
@Entity
@Table(name = "codes", catalog = "web")
public class Codes implements java.io.Serializable {

    private CodesId id;
    private CodesCategory codesCategory;
    private CodesSubcategory subcategory;
    private String description;

    public Codes() {
    }

    public Codes(CodesId id,CodesCategory CodesCategory) {
        this.id = id;
        this.CodesCategory = CodesCategory;
    }

    public Codes(CodesId id, CodesCategory CodesCategory,
            CodesSubcategory subcategory, String description) {
        this.id = id;
        this.CodesCategory = CodesCategory;
        this.subcategory = subcategory;
        this.description = description;
    }

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "codeId", column = @Column(name = "icd_code_id", nullable = false, length = 20)),
            @AttributeOverride(name = "clinicId", column = @Column(name = "clinic_id", nullable = false)) })
    public CodesId getId() {
        return this.id;
    }

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
            @JoinColumn(name = "clinic_id", referencedColumnName = "codes_category_id", nullable = false, insertable = false, updatable = false),
            @JoinColumn(name = "category_id", referencedColumnName = "clinic_id", insertable = false, updatable = false, nullable = false) })
    public CodesCategory getCodesCategory() {
        return this.icdCodesCategory;
    }

    public void setCodesCategory(CodesCategory codesCategory) {
        this.codesCategory = codesCategory;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "subcategory_id")
    public CodesSubcategory getSubcategory() {
        return this.subcategory;
    }

    public void setSubcategory(Subcategory subcategory) {
        this.subcategory = subcategory;
    }

    @Column(name = "description", length = 250)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

而且,这是代码类别实体,

@Entity
@Table(name = "codes_category", catalog = "web")
public class CodesCategory implements java.io.Serializable {

    private CodesCategoryId id;
    private String categoryName;
    private Integer status;
    public CodesCategory() {
    }

    public CodesCategory(CodesCategoryId id) {
        this.id = id;
    }

    public CodesCategory(CodesCategoryId id, String categoryName,
            Integer status) {
        this.id = id;
        this.categoryName = categoryName;
        this.status = status;
    }

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "codesCategoryId", column = @Column(name = "codes_category_id", nullable = false)),
            @AttributeOverride(name = "clinicId", column = @Column(name = "clinic_id", nullable = false)) })
    public CodesCategoryId getId() {
        return this.id;
    }

在我收到的代码中保存或更新时,“category_id不是默认值”。

如何解决这个问题。在此先感谢。

1 个答案:

答案 0 :(得分:0)

有些奇怪的事。你在Codes

中有这个
        @JoinColumn(name = "clinic_id", referencedColumnName = "codes_category_id", nullable = false, insertable = false, updatable = false),
        @JoinColumn(name = "category_id", referencedColumnName = "clinic_id", insertable = false, updatable = false, nullable = false) })

@JoinColumn中,如果name中的Codes列为clinicId,则referencedColumnName应为clinic_id,不应该?它应该是在CodesCategory中保存id的列的名称。

不确定这是主要问题,但似乎是错误。