I have a problem representing this scenario in my code with JPA annotations.
I have the following DB model (all fields are not nullable):
File SourceTypes
----------- -----------
country PK country PK
filename PK sourcetype PK
sourcetype sourcename
The sourcetype field in File, references the SourceTypes table, however both tables have a composite PK, so the foreign key is also composite.
Source Type classes:
@Embeddable
class SoutceTypePk {
String country
String sourcetype
}
@Entity
class SourceType {
@Id
SourceTypePk id;
String sourcename;
}
File classes:
@Embeddable
class FilePk {
country
filename
}
@Entity
class File {
@Id
FilePk id;
@MapsId("country")
@ManyToOne
@JoinColumn(name="country", referencedColumn="country")
@JoinColumn(name="sourcetype", referencedColumn="sourcetype")
SourceType sourceType;
}
- With this configuration, JPA excludes the sourcetype field from insert queries to the File table
- If I use MapsId without specifying the “country” field, I get errors that JPA expected a
“sourcetype” column in the FilePK class - If I take away the MapsId completely then I get “country” field is duplicated
Please help!