简体   繁体   中英

A Foreign key refering MasterDataEntity from MainTableEntity has the wrong number of column

I have a master data table. The use of this table is to just hold the data (as such it has no relationship with other tables).

This is MasterDataEntity

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

@Id
@Column(name = "DESCRIPTION")
private String description;

In one of the other table (a functional table), I want to join this table. Like:

This is the MainTableEntity:

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "TYPE_ID")
private MasterDataEntity masterDataEntity;

The logical intent here is that Maintable should contain one of the Type_Id of the MasterTable (not mandatory of all the rows even).

I do not want to store any kind of information on MainTable in the MasterTable. All I want is only existing values from the master table can be added to the main table.

Apart from the above, I did the following queries to mark the FK relation:

ALTER TABLE MAIN_TABLE ADD TYPE_ID NUMBER;

ALTER TABLE MAIN_TABLE ADD FOREIGN KEY (TYPE_ID ) REFERENCES MASTER_TABLE(TYPE_ID );

With the above code I get the error: A Foreign key referring MasterDataEntity from MainTableEntity has the wrong number of column. should be 2.

I tried looking at the root cause but could not get any helpful info. I am new to Hibernate and it is possible I might be doing wrong things based on my requirements.

Can someone suggest what is wrong here and what should I be doing to achieve my intent?

Thanks

PS: I have mentioned only the relevant info. There are obviously many other columns in MainEntity, while MasterEntity has just 2 columns. Let me know if any info is required.

It might be because of MasterDataEntity have multiple primary key, since id and description fields have @Id Annotation,
It should just have one which is for TYPE_ID :

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

@Column(name = "DESCRIPTION")
private String description;

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