Olá pessoal tenho o seguinte cenário: aluno 1 ----- N aluno_grau N ------ 1 grau, onde a tabela aluno_grau , além dos id’s de aluno e grau, também possui em sua chave composta o atributo data, tendo a referida chave três campos: id_aluno, id_grau e data. Minha dúvida é a seguinte: para persistir um aluno com vários aluno_graus eu posso popular o aluno com vários aluno_graus, ou primeiro tenho que persistir o aluno e separadamento os aluno_grau. estou usando uma classe alunoGrauPK anotada com @Embeddable, como se segue abaixo.
[code]
@Entity
@Table(name = “ALUNOS”)
public class aluno{
//........
@Id
@Column(name = "ID")
private int id = 0;
@OneToMany(mappedBy = "id_aluno", targetEntity=aluno_grau.class)
private List<aluno_grau> aluno_graus;
//…
@Entity
@Table(name = “GRAUS”)
public class grau {
@Id
@Column(name = "ID")
private int id = 0;
@OneToMany(mappedBy = "id_grau", targetEntity=aluno_grau.class)
private List<aluno_grau> aluno_graus;
//…
@Entity
@Table(name = “aluno_graus”)
public class aluno_grau {
@EmbeddedId
protected AlunoGrausPK alunoGrausPK = new AlunoGrausPK();
@Column(name="ID_ALUNO", nullable = false, insertable = false, updatable=false)
private int id_aluno;
@Column(name="ID_GRAU", nullable = false, insertable = false, updatable=false)
private int id_grau;
@Column(name = "DATA", nullable = false, insertable = false, updatable=false)
@Temporal(TemporalType.DATE)
private Date data;
//private
public aluno_grau() {
}
public aluno_grau(AlunoGrausPK alunoGrausPK) {
this.alunoGrausPK = alunoGrausPK;
}
//........
@Embeddable
public class AlunoGrausPK {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="id_aluno")
private aluno aluno;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="id_grau")
private grau grau;
@Basic(optional = false)
private Date data;
public AlunoGrausPK() {
}
public AlunoGrausPK(aluno aluno, grau grau, Date data) {
super();
this.aluno = aluno;
this.grau = grau;
this.data = data;
}
//…
Obs.: Só consigo persistir primeiro o aluno e depois os aluno_grau um a um. O que estou fazendo de errado???