-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Concept Question (Inheritance)
PostPosted: Tue Jun 14, 2005 10:11 am 
Newbie

Joined: Tue Jun 14, 2005 9:48 am
Posts: 3
Hi!
For weeks i'm sofering with a theorical question
I have a class named Pessoa and a class named Advogado who extends Pessoa. I want to persist a Pessoa in the database and after, create a Advogado using this Pessoa. I need to insert Pessoa before because Pessoa can be many things (Cliente, Fornecedor, Funcionario etc...). I tried to create a new Advogado, set the super properties of Advogado with a Pessoa that was in a list (detached) like the following code and when I tried to save() the Advogado, the Pessoa was duplicated in the database so I tried to lock() the pessoa (casting advogado) so the Advogado was not inserted... how to do the inheritance?
I know that Classes have inheritance between them but objects also do?
I'll have to insert Advogado with SQL after insert Pessoa with hibernate?

Code:
        Pessoa pessoaTmp = (Pessoa) pessoas.get(pessoasJList.getSelectedIndex());
        advogado = new Advogado(pessoaTmp);
        JOptionPane.showMessageDialog(this, pessoaTmp.getId());
        advogado.setOab(oabJText.getText());
        try {
            advogadoDAO = new AdvogadoDAO();
            advogado = new Advogado(pessoaTmp);
            advogado.setOab(oabJText.getText());
            advogadoDAO.insert(advogado);
        } ...


Hibernate version:
Hibernate 2.1.8

Mapping documents:
Code:
//Pessoa.java
package br.com.solucoesideais.admOffice.beans;

import java.util.Set;
import java.util.HashSet;

public class Pessoa {

    private Long id;
    private String nome;
    private Set telefones = new HashSet();
    private Set enderecos = new HashSet();
    private Set  emails = new HashSet();
   
    public Pessoa() {
        System.out.println("constutor de pessoa");
    }

    public Long getId() {       
        return id;
    }

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

    public String getNome() {       
        return nome;
    }

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

    public java.util.Set getEnderecos() {
        return enderecos;
    }

    public void addEndereco(Endereco endereco) {
        endereco.setPessoa(this);
//        if (! this.enderecos.contains(endereco)) {
            this.enderecos.add(endereco);
//        }
    }

    public void removeEndereco(Endereco endereco) {
        boolean removed = this.enderecos.remove(endereco);
    }

    public void setEnderecos(Set enderecos) {
        this.enderecos = enderecos;
    }

    public java.util.Set getTelefones() {
        return telefones;
    }

    public void setTelefones(Set telefones) {
        this.telefones = telefones;
    }

    public void addTelefone(Telefone telefone) {
        telefone.setPessoa(this);
        if (! this.telefones.contains(telefone)) {
            this.telefones.add(telefone);
        }
    }

    public void removeTelefone(Telefone telefone) {
        boolean removed = this.telefones.remove(telefone);
    }

    public java.util.Set getEmails() {
        return emails;
    }

    public void setEmails(Set emails) {
        this.emails = emails;
    }

    public void addEmail(Email email) {
        email.setPessoa(this);
        if (! this.emails.contains(email)) {
            this.emails.add(email);
        }
    }

    public void removeEmail(Email email) {
        boolean removed = this.emails.remove(email);
    }
}

Code:
//Advogado.java
package br.com.solucoesideais.admOffice.beans;

public class Advogado extends Pessoa {
   
    private String oab;
   
    public Advogado() {
        super();
    }

    public Advogado(Pessoa pessoa) {
        this.setId(pessoa.getId());
        this.setNome(pessoa.getNome());
        this.setEmails(pessoa.getEmails());
        this.setEnderecos(pessoa.getEnderecos());
        this.setTelefones(pessoa.getTelefones());
    }
   
    public String getOab() {
        return oab;
    }
   
    public void setOab(String oab) {
        this.oab = oab;
    }

    public void setPessoa(Pessoa pessoa) {
        this.setId(pessoa.getId());
        this.setNome(pessoa.getNome());
        this.setEmails(pessoa.getEmails());
        this.setEnderecos(pessoa.getEnderecos());
        this.setTelefones(pessoa.getTelefones());

    }
}

Code:
//AdvogadoDAO.java
package br.com.solucoesideais.admOffice.DAO;
       
import java.util.List;
import java.io.*;
import net.sf.hibernate.*;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
import br.com.solucoesideais.admOffice.beans.Advogado;
import br.com.solucoesideais.admOffice.beans.Pessoa;

public class AdvogadoDAO {

  private SessionFactory sessionFactory;

  public AdvogadoDAO() throws Exception {
    sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
}

 
  public void insert(Advogado advogado) throws Exception {
    Session session = sessionFactory.openSession();
    session.save(advogado);
    session.flush();
    session.close();
  }

  public void update(Advogado advogado) throws Exception {
    Session session = sessionFactory.openSession();
    session.saveOrUpdate(advogado);
    session.flush();
    session.close();
  }

  public void derive(Advogado advogado) throws Exception {
    Session session = sessionFactory.openSession();
    Transaction t = session.beginTransaction();
    Pessoa pessoa = new Pessoa();
    pessoa = (Pessoa) advogado;
    session.lock(pessoa, LockMode.NONE);
    session.saveOrUpdate(advogado);

    t.commit();
    session.close();
  }

  public List getList(String condicao) throws Exception {
    Session session = sessionFactory.openSession();
    List advogados = session.find(condicao);
    session.flush();
    session.close();
    return advogados;
  }

}

Code:
//PessoaDAO.java
package br.com.solucoesideais.admOffice.DAO;
       
import java.util.List;
import net.sf.hibernate.*;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
import br.com.solucoesideais.admOffice.beans.Pessoa;

public class PessoaDAO {

  private SessionFactory sessionFactory;

  public PessoaDAO() throws Exception {
    System.out.println("Cheguei aqui");
    sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    System.out.println("Passei daqui");
}

 
  public void insert(Pessoa pessoa) throws Exception {
    Session session = sessionFactory.openSession();
    session.save(pessoa);
    session.flush();
    session.close();
  }

  public void update(Pessoa pessoa) throws Exception {
    Session session = sessionFactory.openSession();
    session.saveOrUpdate(pessoa);
    session.flush();
    session.close();
  }

  public List getList(String condicao) throws Exception {
    Session session = sessionFactory.openSession();
    List pessoas = session.find(condicao);
    session.flush();
    session.close();
    return pessoas;
  }

}


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
NO EXCEPTIONs

Name and version of the database you are using:
mysql 4.17

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 10:53 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use the regular forum.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.