I am using java and hibernate annotations to define the database schema and want to specify the foreign key in one table as the primary key.
I am getting an error when I set this up and think it could be down to how I am setting up the foreign key as the primary key because when I use a normal primary key I don't get an error.
What is the correct way to set up a foreign key as the primary key?
My current code set up is :
@Table(name="BATCH_STEP_EXECUTION_CONTEXT")
public class BatchStepExecutionContext implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JoinColumn(name = "STEP_EXECUTION_ID" , columnDefinition="BIGINT NOT NULL", referencedColumnName="STEP_EXECUTION_ID")
@ForeignKey(name="STEP_EXEC_CTX_FK ")
@IndexColumn(name="IDX_STEP_EXEC_CTX")
private BatchStepExecution batchStepExecution;
and is referenced by the Batch Step Execution table as:
// bi-directional many-to-one association to Batch Step Execution Context
@OneToMany(mappedBy = "batchStepExecution", cascade = {CascadeType.ALL})
private List<BatchStepExecutionContext> batchStepExecutionContext;
the error I'm getting when I try to run the code is:
Unable to read the mapped by attribute for batchStepExecutionContext in com.ccs.nbook.domain.model.BatchStepExecutionContext!
The tables I'm trying to model in the java code are:
CREATE TABLE BATCH_STEP_EXECUTION
(
STEP_EXECUTION_ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (MAXVALUE 9223372036854775807),
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR (100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
STATUS VARCHAR (10),
COUNT BIGINT,
CONSTRAINT JOB_EXEC_STEP_FK FOREIGN KEY (JOB_EXECUTION_ID) REFERENCES BATCH_JOB_EXECUTION (JOB_EXECUTION_ID),
PRIMARY KEY (STEP_EXECUTION_ID)
)
;
CREATE TABLE BATCH_STEP_EXECUTION_CONTEXT
(
STEP_EXECUTION_ID BIGINT NOT NULL,
SHORT_CONTEXT VARCHAR (2500) NOT NULL,
SERIALIZED_CONTEXT LONG VARCHAR,
PRIMARY KEY (STEP_EXECUTION_ID),
CONSTRAINT STEP_EXEC_CTX_FK FOREIGN KEY (STEP_EXECUTION_ID) REFERENCES BATCH_STEP_EXECUTION (STEP_EXECUTION_ID)
)
;
So I am trying to model the relationship of STEP_EXECUTION_ID between both tables where it is a primary key in BATCH_STEP_EXECUTION and is a primary key and foreign key in BATCH_STEP_EXECUTION_CONTEXT