In my application I have 3 core classes as described below.
public class Semana {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer series;
private String observacao;
private Integer ordenacao;
@OneToMany(mappedBy = "semana")
@JsonBackReference
@OrderBy("ordenacao asc ")
private List<TreinoOrdenado> treinoOrdenados;
}
public class TreinoOrdenado {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "id_semana")
private Semana semana;
@ManyToOne
@JoinColumn(name = "id_treino")
private Treino treino; // can be repeated in the database but with different "ordenacao"
private Integer ordenacao;
}
@Table(name = "exercicio_ordenado")
public class ExercicioOrdenado {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "id_treino")
@JsonBackReference
@ToString.Exclude
private Treino treino;
@ManyToOne
@JoinColumn(name = "id_exercicio")
private Exercicio exercicio;
private Integer ordenacao;
@ManyToOne
@JoinTable(name = "parametro")
private Parametro parametro;
}
And this Parametro class which is a relationship between those 3 classes.
public class Parametro {
@EmbeddedId
private ParametroId parametroId = new ParametroId();
@MapsId("semana")
@OneToOne
@JoinColumn(name = "semana_id")
private Semana semana;
@MapsId("treino")
@OneToOne
@JoinColumn(name = "treino_id")
private TreinoOrdenado treino;
@MapsId("exercicio")
@OneToOne
@JoinColumn(name = "exercicio_id")
private ExercicioOrdenado exercicio;
private Integer series;
private String repeticoes;
private String observacao;
}
public class ParametroId implements Serializable {
@Column(name = "semana_id")
private Long semana;
@Column(name = "treino_id")
private Long treino;
@Column(name = "exercicio_id")
private Long exercicio;
}
So, here is my problem. I want to be able to access the Parametro from it's parent class ExercicioOrdenado, but in order to be distinguishable in the database the class Parametro needs the reference from the other two (Semana, TreinoOrdenado).
With my current mapping I got this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Foreign key (FK73ewcy2r50kw71o4he51dkolv:parametro
[parametro_exercicio_id,parametro_semana_id,parametro_treino_id])) must have same number of columns as the referenced primary key (parametro [id])
I can persist paremetros in the database with:
parametros.setSemana(semana);
parametros.setExercicio(exercicioOrdenado);
parametros.setTreino(treinoOrdenado);
parametroRepository.save(parametros);
But I can't retrieve from ExercicioOrdenado with the mapping.
I don't know the right approach to do this.