Hibernate's OneToMany/JoinTable with a Composite Key
I am having an issue with using the @JoinTable annotation when using a Composite Key in a One-To-Many relationship. I am using the Quarkus framework. The current error I am facing is 'Repeated column in mapping for collection'. This is because I am using duck_id in both joinColumns and inverseJoinColumns within the @JoinTable. Duck_id needs two be in both places.
Is there something different about using composite keys with Quarkus that I am missing? I have researched that Quarkus does support it, but I could not find any example using the JoinTable annotation.
It is important to note that I don't have a way to modify the database tables manually.
Tables
These two tables are in this scope.
- duck
- duck_mission
The second table 'duck_mission' has only two columns, 'duck_id' and 'mission_id' in which both columns are foreign keys to their corresponding tables. The two foreign keys together make up the primary key of table 'duck_mission'. This table uses a CompositeKey to satisfy the requirement that all Entities MUST have a primary key. Note: mission_id is a foreign key to another table not listed here called 'Mission'.
ENTITIES
DUCK ENTITY
@Entity
@Table(name = "duck")
public class DuckEntity extends PanacheEntityBase {
@Id
@GeneratedValue
@Column(name = "id")
public UUID id;
@OneToMany
@JoinTable(
name = "duck_mission",
joinColumns = @JoinColumn(name = "duck_id"),
inverseJoinColumns = {
@JoinColumn(name = "duck_id", referencedColumnName = "duckId"),
@JoinColumn(name = "mission_id", referencedColumnName = "missionId")
}
)
public List<DuckMissionEntity> duckMissions;
//Other columns, getters, and setters
}
DUCK MISSION ENTITY
@Data
@Embeddable
class DuckMissionEntityId implements Serializable {
@Column(name = "duck_id")
public UUID duckId;
@Column(name = "mission_id")
public UUID missionId;
}
@Entity
@Table(name = "duck_mission")
public class DuckMissionEntity extends PanacheEntityBase {
// The Composite Key
@EmbeddedId
public DuckMissionEntityId id;
public UUID duckId;
public UUID missionId;
//Other Getters and Setters
}
I have tried using @IdClass instead of an embeddedId, but it seems there is no difference between them. I also have tried substituting other values in place of the joinColumns.
If I add (nullable = false, insertable=false, updatable=false) to the inverseJoinColumns, I then get the error 'null id generated for:class'.
In
DuckMissionEntityyou already mappedduckIdandmissionIdviaDuckMissionEntityId idso no needs to redeclare them as separate properties.