Olá pessoal.. estou tentando aprender a utilizar o Hibernate mas estou parado na seguinte questão:
Tenho a classe aluno com as seguintes propriedades:
package model;
import java.util.Collection;
public class Aluno {
private Long idAluno;
private String nmAluno;
private Endereco endereco;
private Collection tarefas;
public Aluno() {
}
public Endereco getEndereco() {
return endereco;
}
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
public Long getIdAluno() {
return idAluno;
}
public void setIdAluno(Long idAluno) {
this.idAluno = idAluno;
}
public String getNmAluno() {
return nmAluno;
}
public void setNmAluno(String nmAluno) {
this.nmAluno = nmAluno;
}
public Collection getTarefas() {
return tarefas;
}
public void setTarefas(Collection tarefas) {
this.tarefas = tarefas;
}
}
A classe tarefa:
package model;
public class Tarefa {
private Long idTarefa;
private Long idAluno;
private String nmTarefa;
public Tarefa() {
}
public Long getIdTarefa() {
return idTarefa;
}
public void setIdTarefa(Long idTarefa) {
this.idTarefa = idTarefa;
}
public String getNmTarefa() {
return nmTarefa;
}
public void setNmTarefa(String nmTarefa) {
this.nmTarefa = nmTarefa;
}
public Long getIdAluno() {
return idAluno;
}
public void setIdAluno(Long idAluno) {
this.idAluno = idAluno;
}
}
Sendo que um aluno pode ter várias tarefas. Estou fazendo o mapeamento assim:
Aluno:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="model">
<class name="Aluno" table="aluno">
<id name="idAluno" column="id_aluno" type="long">
<generator class="sequence">
<param name="sequence">sqc_aluno</param>
</generator>
</id>
<property name="nmAluno" column="nm_aluno" type="string" not-null="false" length="100"/>
<set name="tarefas" inverse="true" lazy="true" cascade="save-update">
<key column="id_aluno"/>
<one-to-many class="Tarefa"/>
</set>
</class>
</hibernate-mapping>
Tarefa:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="model">
<class name="Tarefa" table="tarefa">
<id name="idTarefa" column="id_tarefa" type="long">
<generator class="sequence">
<param name="sequence">sqc_tarefa</param>
</generator>
</id>
<property name="nmTarefa" column="nm_tarefa" type="string" not-null="false" length="100"/>
</class>
</hibernate-mapping>
Ele insere no banco o aluno certinho.. insere as tarefas mas não insere o aluno que as tarefas pertencem:
Aluno
id_aluno: 1
nome: Diego Gomes
Tarefa
id_tarefa: 1
id_aluno: aqui está o problema.. ele não identifica o aluno.
nm_tarefa: Fazer redação.
Estou fazendo assim para testar:
package main;
import java.util.HashSet;
import model.Aluno;
import model.Tarefa;
import util.DAOFactory;
import dao.GenericDAO;
public class Main {
public static void main(String[] args) {
Aluno aluno = new Aluno();
Tarefa tarefa1 = new Tarefa();
tarefa1.setNmTarefa("Ler apostila de biologia");
Tarefa tarefa2 = new Tarefa();
tarefa2.setNmTarefa("Fazer redação");
aluno.setTarefas(new HashSet());
aluno.getTarefas().add(tarefa1);
aluno.getTarefas().add(tarefa2);
aluno.setNmAluno("Diego Gomes");
GenericDAO dao = DAOFactory.getDAO("aluno");
try {
dao.save(aluno);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Alguém pode me dar uma ajuda:
