Lipe, veja o que eu fiz no meu código, porém, não sei se é normal mas ele criou mas uma tabela no banco de dados aluno_estagio, pelo o que entendi é uma tabela associativa.
/*
* Estagio.java
*
* Created on 13 de Maio de 2005, 10:02
*/
package syslagg.beans;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.*;
@Entity(access=AccessType.FIELD)
@Table(name="Estagio")
@SequenceGenerator(name="SEQ_ESTAGIO", sequenceName="SEQ_ESTAGIO")
public class Estagio implements Serializable {
@Id(generate=GeneratorType.SEQUENCE, generator="SEQ_ESTAGIO")
private int id_estagio;
@Column(name="nome", nullable=false, length=50)
private String nome;
@Column(name="descricao", nullable=false, length=255)
private String descricao;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Collection<syslagg.beans.Aluno> alunos;
public Collection<syslagg.beans.Aluno> getAluno(){
return this.alunos;
}
public void setAluno(Collection<syslagg.beans.Aluno> aluno){
this.alunos = aluno;
}
public void setId (int id){
this.id_estagio = id;
}
public int getId(){
return this.id_estagio;
}
public void setNome(String nome){
this.nome = nome;
}
public String getNome(){
return this.nome;
}
public void setDescricao(String descricao){
this.descricao = descricao;
}
public String getDescricao(){
return this.descricao;
}
}
Aluno.java.
package syslagg.beans;
import java.io.Serializable;
import javax.persistence.*;
@Entity(access=AccessType.FIELD)
@Table(name="Aluno")
public class Aluno implements Serializable {
@Id(generate=GeneratorType.NONE)
private int ra; // Ra do Aluno
@Column(name="nome", nullable=false, length=60)
private String nome; // Nome completo do Aluno
@Column(name="email", nullable=true, length=50)
private String email; // Endereço de e-mail
@Column(name="telefone", nullable=true, length=14)
private String telefone; // Telefone para contato
@Column(name="celular", nullable=true, length=14)
private String celular; // Celular para contato
@Column(name="curso", nullable=false, length=255)
private String curso; // Curso que faz
@Column(name="serie", nullable=false, length=1)
private String serie; // Série que cursa
@Column(name="campus", nullable=false, length=50)
private String campus; // Campus onde estuda
public void setRa (int ra){
this.ra = ra;
}
int getRa(){
return this.ra;
}
public void setNome (String nome){
this.nome = nome;
}
String getNome (){
return this.nome;
}
public void setEmail(String email){
this.email = email;
}
String getEmail(){
return this.email;
}
public void setTelefone(String telefone){
this.telefone = telefone;
}
String getTelefone(){
return this.telefone;
}
public void setCelular(String celular){
this.celular = celular;
}
String getCelular(){
return this.celular;
}
public void setCurso(String curso){
this.curso = curso;
}
String getCurso(){
return this.curso;
}
public void setSerie(String serie){
this.serie = serie;
}
String getSerie(){
return this.serie;
}
public void setCampus(String campus){
this.campus = campus;
}
String getCampus(){
return this.campus;
}
}
Veja o que eu fiz agora na minha classe:
Estagio.java
/*
* Estagio.java
*
* Created on 13 de Maio de 2005, 10:02
*/
package syslagg.beans;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import javax.persistence.*;
@Entity(access=AccessType.FIELD)
@Table(name="Estagio")
@SequenceGenerator(name="SEQ_ESTAGIO", sequenceName="SEQ_ESTAGIO")
public class Estagio implements Serializable {
@Id(generate=GeneratorType.SEQUENCE, generator="SEQ_ESTAGIO")
private int id_estagio;
@Column(name="nome", nullable=false, length=50)
private String nome;
@Column(name="descricao", nullable=false, length=255)
private String descricao;
/**
* Criando um relacionamento entre as
* classes Estagio e Aluno
**/
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="id_estagio")
private Set<Aluno> alunos;
public void setId (int id){
this.id_estagio = id;
}
public int getId(){
return this.id_estagio;
}
public void setNome(String nome){
this.nome = nome;
}
public String getNome(){
return this.nome;
}
public void setDescricao(String descricao){
this.descricao = descricao;
}
public String getDescricao(){
return this.descricao;
}
}
Até ai tudo blz, porém, não consigo passar daqui, tenho que criar um relacionamento Many-to-one na classe Aluno que se relaciona com o Estagio, e tipo como faço para salvar um aluno e definir o seu tipo de estágio.