Ola amigos estou aqui denovo com um problema em um CRUD no hibernate q estou tentando fazer.
QUando tento cadastrar da um erro dizendo: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): br.com.negocio.Endereco
Aqui vai as minhas classes de criação do crud:/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.dados.hibernate;
import br.com.negocio.Aluno;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
/**
*
* @author Júnior
*/
public class AlunoDAO {
private SessionFactory factory;
public AlunoDAO(){
this.factory = new AnnotationConfiguration().configure().buildSessionFactory();
}
public void inserirAluno(Aluno aluno){
Session session = this.factory.openSession();
Transaction tx = session.beginTransaction();
session.save(aluno);
session.flush();
tx.commit();
session.close();
}
public boolean existe(String cpf, String tipo){
Session session = this.factory.openSession();
Aluno aluno = (Aluno) session.get(Aluno.class, cpf);
boolean exi = session.contains(aluno);
session.flush();
session.close();
return exi;
}
public void removerAluno(String cpf){
Session session = this.factory.openSession();
Aluno alu = (Aluno) session.get(Aluno.class, cpf);
Transaction tx = session.beginTransaction();
session.delete(alu);
session.flush();
tx.commit();
session.close();
}
public Aluno procurarAluno(String cpf){
Session session = this.factory.openSession();
Aluno aluno = (Aluno) session.get(Aluno.class, cpf);
session.flush();
//session.close();
return aluno;
}
public void atualizarAluno(Aluno aluno){
Session session = this.factory.openSession();
Transaction tx = session.beginTransaction();
session.update(aluno);
session.flush();
tx.commit();
session.close();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.dados.hibernate;
import br.com.negocio.Aluno;
import br.com.negocio.IRepositorioAlunos;
import java.sql.SQLException;
/**
*
* @author Júnior
*/
public class RepositorioAlunosHibernate implements IRepositorioAlunos {
private AlunoDAO alunoDao;
public RepositorioAlunosHibernate() {
this.alunoDao = new AlunoDAO();
}
public void inserir(Aluno aluno) throws SQLException {
alunoDao.inserirAluno(aluno);
}
public void remover(String cpf) throws SQLException {
alunoDao.removerAluno(cpf);
}
public boolean existe(String cpf, String tipo) throws SQLException {
return alunoDao.existe(cpf, tipo);
}
public Aluno procurar(String cpf) throws SQLException {
return alunoDao.procurarAluno(cpf);
}
public void atualizar(Aluno aluno) throws SQLException {
alunoDao.atualizarAluno(aluno);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.dados.hibernate;
import br.com.negocio.Professor;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
/**
*
* @author Júnior
*/
public class ProfessorDAO {
private SessionFactory factory;
public ProfessorDAO(){
this.factory = new AnnotationConfiguration().configure().buildSessionFactory();
}
public void inserirProfessor(Professor professor){
Session session = this.factory.openSession();
Transaction tx = session.beginTransaction();
session.save(professor);
session.flush();
tx.commit();
session.close();
}
public boolean existe(String cpf, String tipo){
Session session = this.factory.openSession();
Professor professor = (Professor) session.get(Professor.class, cpf);
boolean exi = session.contains(professor);
session.flush();
session.close();
return exi;
}
public void removerProfessor(String cpf){
Session session = this.factory.openSession();
Professor pro = (Professor) session.get(Professor.class, cpf);
Transaction tx = session.beginTransaction();
session.delete(pro);
session.flush();
tx.commit();
session.close();
}
public Professor procurarProfessor(String cpf){
Session session = this.factory.openSession();
Professor Professor = (Professor) session.get(Professor.class, cpf);
session.flush();
//session.close();
return Professor;
}
public void atualizarProfessor(Professor professor){
Session session = this.factory.openSession();
Transaction tx = session.beginTransaction();
session.update(professor);
session.flush();
tx.commit();
session.close();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.dados.hibernate;
import br.com.negocio.IRepositorioProfessores;
import br.com.negocio.Professor;
import java.sql.SQLException;
/**
*
* @author Júnior
*/
public class RepositorioProfessoresHibernate implements IRepositorioProfessores{
private ProfessorDAO professorDao;
public RepositorioProfessoresHibernate() {
this.professorDao = new ProfessorDAO();
}
public void inserir(Professor professor) throws SQLException {
professorDao.inserirProfessor(professor);
}
public void remover(String cpf) throws SQLException {
professorDao.removerProfessor(cpf);
}
public boolean existe(String cpf, String tipo) throws SQLException {
return professorDao.existe(cpf, tipo);
}
public Professor procurar(String cpf) throws SQLException {
return professorDao.procurarProfessor(cpf);
}
public void atualizar(Professor professor) throws SQLException {
professorDao.atualizarProfessor(professor);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.negocio;
import java.io.Serializable;
import javax.persistence.*;
/**
*
* @author Júnior
*/
@Entity
@Table(name="endereco")
public class Endereco implements Serializable{
@Id
@Column(name="cpf")
private String cpf;
@Column
private String cep;
@Column
private String numero;
@Column
private String complemento;
public Endereco(String cep, String numero, String complemento) {
this.cep = cep;
this.numero = numero;
this.complemento = complemento;
}
public Endereco(String cpf, String cep, String numero, String complemento) {
this.cpf = cpf;
this.cep = cep;
this.numero = numero;
this.complemento = complemento;
}
public Endereco(){
}
public String getComplemento() {
return complemento;
}
public void setComplemento(String complemento) {
this.complemento = complemento;
}
public String getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
}
Ajudem Please !!!