Dúvida sobre Relacionamento no Hibernate 3

Olá Pessoal,

Estou tentando configurar um relacionamento entre duas classes no hibernate, estou usando o annotations, vejam minhas classes:

Classe Aluno

package syslagg.beans;

import javax.persistence.*;


@Entity(access=AccessType.FIELD)
@Table(name="Aluno")
public class Aluno{
    @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
    
    @Column(name="estagio", nullable=false)
    private int estagio;     // Tipo de estágio

        
    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;
    }
    
    public void setEstagio (int estagio){
        this.estagio = estagio;
    }
    
    
    public int getEstagio (){
        return this.estagio;
    }
}

Classe Estagio

/*
 * Estagio.java
 *
 * Created on 13 de Maio de 2005, 10:02
 */

package syslagg.beans;

import javax.persistence.*;


@Entity(access=AccessType.FIELD)
@Table(name="Estagio")
@SequenceGenerator(name="SEQ_ESTAGIO", sequenceName="SEQ_ESTAGIO")
public class Estagio {

    @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;
    
   
    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;
    }
}

E o relacionamento que eu quero

Bom se alguem puder me ajudar a criar este relacionamento nas classes.

Valeu,

Paulo

http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#d0e661

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;
    }
    
}

Valeu,

Paulo

Cadê a join column?

Me diz uma coisa, ele serve para não criar mais uma tabela no relacionamento?

Valeu,
Paulo

Tabelas de relacionamento servem para n…n. Não é o seu caso.

Como você não declarou a FK o Hibernate criou essa tabela relacionando idEstagio com idAluno.

Lipe,

Valeu kra, porém vc tem algum exemplo ai para q eu possa colocar aqui na minha classe.

Valeu,

Paulo

Lipe,

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.

Valeu

Paulo

Cara, lê o link que eu te passei…

Valeu Lipe, consegui ficou massa.

T+
Paulo