Problemas com hibernate

1 resposta
G

Olá a todos,

Estou tentando fazer um relacionamento entre um entidade Turma e Alunos de NxN,

meus mapeamentos são os seguintes:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="br.com.ebrand" default-lazy="false">
	<class name="Aluno" table="aluno">
		<id name="id" column="Cod_Aluno">
                  <generator class="identity"/>
		</id>
                <property name="nome" column="Alu_Nome" length="100" not-null="true"/>
                <property name="email" column="Alu_Email" unique="true" length="100" not-null="true"/>
                <bag name="turmas" table="TurmaAluno" lazy="false">
                    <key column="Cod_Aluno"/>
                    <many-to-many class="Turma" column="Cod_Turma"/>
                </bag>
	</class>
</hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="br.com.ebrand" default-lazy="false">
	<class name="Turma" table="turma">
		<id name="id" column="Cod_Turma">
                  <generator class="identity"/>
		</id>
                <property name="nome" column="Tur_Nome" unique="true" length="100" not-null="true"/>                
                <bag name="alunos" table="TurmaAluno" lazy="false">
                    <key column="Cod_Turma"/>
                    <many-to-many class="Aluno" column="Cod_Aluno"/>
                </bag>
	</class>
</hibernate-mapping>

Já as classes relacionadas a esses mapeamentos são

package teste;

import java.util.*;

public class Aluno {
    private Long id;
    private String nome;
    private String email;
    private List turmas;

    public Aluno() {
    }

    public Aluno(String nome, String email){
        this.nome = nome;
        this.email = email;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return this.id;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getNome() {
        return this.nome;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return this.email;
    }

    
    public void setTurmas(List turmas) {
        this.turmas = turmas;
    }

    public List getTurmas() {
        return this.turmas;
    }
}
package br.com.ebrand;

import java.util.*;

public class Turma {
    private Long id;
    private String nome;
    private List alunos;

    public Turma() {
    }

    public Turma(String nome){
        this.nome = nome;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return this.id;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getNome() {
        return this.nome;
    }
    
    public void setAlunos(List alunos) {
        this.alunos = alunos;
    }

    public List getAlunos() {
        return this.alunos;
    }
}
estou tentando relizar a seguinte operacao
Aluno alu = AlunoDAO.buscarAluno(new Long(5));
Turma tur = new Turma("Turma 1");
tur.setAlunos(new ArrayList());
tur.getAlunos().add(alu);
s.save(tur);

onde o metodo buscarAluno, traz um aluno do banco para eu relacionar com uma turma.

O problema é q sempre esta retornando uma Exception:
org.hibernate.HibernateException: Session is closed

se alguem puder me ajudar fico grato.

Anderson.

1 Resposta

A

Ta faltando vc abrir a sessao!

Criado 21 de agosto de 2005
Ultima resposta 22 de ago. de 2005
Respostas 1
Participantes 2