I have a junction table to represent an M:N relationship between person and film called “Cast.”
@Embeddable
public class CastId {
@Column(name = "person_id")
private Integer personId;
@Column(name = "film_id")
private Integer filmId;
@Entity
@Table(name = "film_cast")
public class Cast {
@EmbeddedId
private CastId id;
@ManyToOne
@MapsId("personId")
@JoinColumn(name = "person_id")
private Person person;
@ManyToOne
@MapsId("filmId")
@JoinColumns({
@JoinColumn(name="film_id", referencedColumnName="filmId"),
@JoinColumn(name="film_type", referencedColumnName="filmType")
})
private Film film;
Film has a composite key:
@Embeddable
public class FilmId {
@Column(name = "film_id")
private Integer filmId;
@Column(name = "film_type")
private Integer filmType;
This code is giving me an error, “Failed to initialize JPA EntityManagerFactory: Property ‘film’ of entity ‘com.name.projname.model.Cast’ must have a ‘@JoinColumn’ which references the foreign key column ‘film_id'”.
I tried just doing
@ManyToOne
@MapsId("personId")
@JoinColumn(name = "person_id")
private Person person;
@ManyToOne
@MapsId("filmId")
@JoinColumn(name = "film_id")
private Film film;
but that does not work. It seems like my issue is trying to add a foreign key to a composite key, and my formatting is done wrong.
Ray Raiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.