-->
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.  [ 3 posts ] 
Author Message
 Post subject: Concept Question (Inheritance)
PostPosted: Tue Jun 14, 2005 10:43 pm 
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());

    }
}


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

}


Full stack trace of any exception that occurs:
NO EXCEPTIONs

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 11:09 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Post your mapping files to provide a better picture if your configuration.


Top
 Profile  
 
 Post subject: Sorry
PostPosted: Wed Jun 15, 2005 7:59 am 
Newbie

Joined: Tue Jun 14, 2005 9:48 am
Posts: 3
Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

    <hibernate-mapping>
        <class name="br.com.solucoesideais.admOffice.beans.Pessoa" table="pessoas">
            <id name="id" column="id" type="java.lang.Long" unsaved-value="null">
                <generator class="native"/>
            </id>
            <property name="nome" type="java.lang.String" column="nome" length="100" not-null="true"/>

            <set name="telefones" inverse="true" cascade="all-delete-orphan">
                <key column="pessoa_id"/>
                    <one-to-many class="br.com.solucoesideais.admOffice.beans.Telefone"/>
            </set>

            <set name="emails" inverse="true" cascade="all-delete-orphan">
                <key column="pessoa_id"/>
                    <one-to-many class="br.com.solucoesideais.admOffice.beans.Email"/>
            </set>

            <set name="enderecos" inverse="true" cascade="all-delete-orphan">
                <key column="pessoa_id"/>
                    <one-to-many class="br.com.solucoesideais.admOffice.beans.Endereco"/>
            </set>
        </class>


        </hibernate-mapping>
   


Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

    <hibernate-mapping>
            <joined-subclass name="br.com.solucoesideais.admOffice.beans.Advogado" extends = "br.com.solucoesideais.admOffice.beans.Pessoa" table="advogados">
                <key column="pessoa_id"/>

                <property name="oab" type="java.lang.String" column="oab"/>

            </joined-subclass>           
    </hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.