Skip to content

Hibernate join columns error: Unable to find column with logical name

I am still pretty much a hibernate newb. I am trying to solve an issue when I start my server:

org.hibernate.MappingException: Unable to find column with logical name: organization_id in spe_workflow

Entities as below:

@Entity
@Table(name = "sep_pr_req_att")
public class PrReqAtt implements Serializable, IPojo {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator="hiloen")
    @GenericGenerator(name="hiloen", strategy = "org.hibernate.id.enhanced.TableGenerator")
    @Basic(optional = false)
    @Column(name = "uuid")
    private Integer uuid;

    @JoinColumn(name = "pr_req_id", referencedColumnName = "uuid")
    @ManyToOne(optional = false)
    private PrReq prReqId;
    
    // error happened after adding this attribute
    @JoinColumns({
        @JoinColumn(
            name = "stage",
            referencedColumnName = "id"),
        @JoinColumn(
            name = "organization_id",
            referencedColumnName = "organization_id")
    })
    @ManyToOne
    private ProcessFlow processFlow;

    // ...
}

@Entity
@Table(name = "spe_workflow")
public class ProcessFlow implements Serializable, IPojo {

    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @JoinColumn(name = "organization_id", referencedColumnName = "uuid", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Organization organization;

    // ...
}

SQL Files:

CREATE TABLE `sep_pr_req_att`  (
  `uuid` int(11) NOT NULL,
  `pr_req_id` int(11) NOT NULL,
  `stage` int(11) NULL DEFAULT NULL,
  `organization_id` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`uuid`) USING BTREE,
  INDEX `pr_req_att_fk1`(`pr_req_id`) USING BTREE,
  INDEX `FKE35948C46A2C65EF`(`pr_req_id`) USING BTREE,
  INDEX `pr_req_att_fk2`(`stage`) USING BTREE,
  INDEX `pr_req_att_fk3`(`organization_id`) USING BTREE,
  CONSTRAINT `FKE35948C46A2C65EF` FOREIGN KEY (`pr_req_id`) REFERENCES `sep_pr_req` (`uuid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `pr_req_att_fk1` FOREIGN KEY (`pr_req_id`) REFERENCES `sep_pr_req` (`uuid`) ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `pr_req_att_fk2` FOREIGN KEY (`stage`) REFERENCES `spe_workflow` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `pr_req_att_fk3` FOREIGN KEY (`organization_id`) REFERENCES `spe_workflow` (`organization_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;

CREATE TABLE `spe_workflow`  (
  `id` int(11) NOT NULL,
  `organization_id` int(11) NOT NULL,
  PRIMARY KEY (`id`, `organization_id`) USING BTREE,
  INDEX `spe_workflow_fk1`(`organization_id`) USING BTREE,
  INDEX `id`(`id`) USING BTREE,
  CONSTRAINT `spe_workflow_fk1` FOREIGN KEY (`organization_id`) REFERENCES `om_organization` (`uuid`) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;

Error happened after adding two new columns stage and organization_id in sep_pr_req_att which is suppose to join to ProcessFlow entity/ spe_workflow table. I am referring to an existing similar table structure on the mapping. I believe I am missing something here. What am I doing wrong?

Answer

You spe_workflow table has a composite primary key, so you should correct your mapping accordingly. There are several possible approaches.

For example you can use @IdClass:

@Entity
@Table(name = "spe_workflow")
@IdClass(ProcessFlowPK.class)
public class ProcessFlow implements IPojo {

    @Id
    @Column(name = "id")
    private Integer id;

    @Id
    @Column(name = "organization_id")
    private Integer organizationId;
  
    // ...
}

public class ProcessFlowPK implements Serializable {

    private Integer id;
    private Integer organizationId;

    public ProcessFlowPK() {
    }

    public ProcessFlowPK(Integer id, Integer organizationId) {
        this.id = id;
        this.organizationId = organizationId;
    }

    // equals , hashCode , getters , setters are omitted for brevity
}