Would someone be able to show where I am going wrong in the relation between these two example entities: DuckEntity
and DuckMissionEntity
? The relation is a One-To-Many where one Duck can have many DuckMissions.
DuckMissionEntity has a composite key combining columns duck_id
and mission_id
. Both of those IDs are also foreign keys on the table.
I am getting this error when trying to persist DuckEntity:
org.postgresql.util.PSQLException: ERROR: column "duckentity_id" of relation "duck_mission" does not exist
The error makes sense because duckentity is not supposed to be a column on the duck_mission
table but it is just a Many-To-One mapped from the DuckMissionEntity
. So I am wondering if I am mapping everything correctly. I had a look at the documentation and looked to many other sources for help, but can’t seem to pinpoint where I am going wrong.
My end goal is to be able to persist all of the DuckMissions whenever I persist one single DuckEntity, so they are saved tot he database at the same time.
Here are my example entities:
DuckEntity
@Entity
@Table(name = "duck", schema = "si")
public class DuckEntity extends PanacheEntityBase {
@Id
@GeneratedValue
@Column(name = "id")
public UUID id;
@OneToMany(mappedBy = "duckEntity", cascade = CascadeType.ALL, orphanRemoval = true)
public List<DuckMissionEntity> duckMissions;
public void setDuckMissions(List<DuckMissionEntity> duckMissionEntities){
this.duckMissions = duckMissionEntities;
}
@Transactional
public static void save(DuckEntity duck) {
try {
duck.persist();
if (!duck.isPersistent()) {
LOGGER.error("DuckEntity {} was not persisted to DB", duck.id);
}
} catch(Exception e) {
LOGGER.error("Could Not Save Duck", e);
}
}
}
DuckMissionEntity
@Data
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
class DuckMissionEntityId implements Serializable {
@Column(name = "duck_id")
public UUID duckId;
@Column(name = "mission_id")
public UUID missionId;
}
@Entity
@Table(name = "duck_mission", schema = "si")
@NoArgsConstructor
@AllArgsConstructor
public class DuckMissionEntity extends PanacheEntityBase {
@EmbeddedId
public DuckMissionEntityId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("duckId")
public DuckEntity duckEntity;
public DuckMissionEntity(UUID duckId, UUID missionId) {
this.id = new DuckMissionEntityId(duckId, missionId);
}
}