简体   繁体   中英

ManyToOne Multiple Join Column in Hibernate

I have 2 tables, one is codes_category , another one is codes. I am creating a two join column in codes table, that two column is primary key in code_category table. Below is the entity of codes Table.

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

And, this is the Codes Category Entity,

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

While saving or updating in Codes i am getting, "category_id does not default value".

How to solve this problem. Thanks in Advance.

There is something strange. You have this in 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) })

In the @JoinColumn , if the column name in Codes is clinicId , the referencedColumnName should be clinic_id , shouldn't it ? It is supposed to be the name of the column that holds the id in CodesCategory .

Not sure this is the main problem but it seems like an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM