Herança com JPA JOINED, está correto?

0 respostas
D

Olá pessoal!

Estou fazendo um projeto, utilizando como BD o PostgreSQL e JPA.
Eu tenho:
Classe mãe: Pessoa
Classe filhas: Paciente

Irei utilizar para representar a herança no BD:
[color=red]Joined subclass: [/color]A raiz da hierarquia é representada por uma tabela e cada subclasse é representada por uma tabela separada que contém campos específicos para a subclasse e também as colunas que representam as suas chaves primárias.

O que eu gostaria de saber é [color=red]se está correta a forma que eu fiz[/color], para então poder dar continuidade ao meu projeto. Por favor, vcs podem dar uma olhada? [color=green]Estou postando tanto as minhas classes, quanto o código SQL gerado para a criação das tabelas do BD.[/color] Agradeço desde já.

Classe Pessoa
package heranca;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Persistence;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Pessoa implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String nome;
    private String telefone;

    public EntityManager criaEntidade(){
        EntityManagerFactory fabricaDAO = Persistence.createEntityManagerFactory("herancaTestePU");
        EntityManager dao = fabricaDAO.createEntityManager();
        return dao;
    }

    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 String getTelefone() {         return telefone;     }

    public void setTelefone(String telefone) {         this.telefone = telefone;     }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Pessoa)) {
            return false;
        }
        Pessoa other = (Pessoa) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "heranca.Pessoa[id=" + id + "]";
    }

}
Classe Paciente
package heranca;

import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;

@Entity
@PrimaryKeyJoinColumn(name="Pessoa_id_Pessoa", referencedColumnName="id")

public class Paciente extends Pessoa {
    private static final long serialVersionUID = 1L;

    private String curso;
    private String ano;

    public String getAno() {     return ano;    }

    public void setAno(String ano) {      this.ano = ano;     }

    public String getCurso() {      return curso;     }

    public void setCurso(String curso) {       this.curso = curso;     }
}
Ao chamar o método criaEntidade, foi criado as tabelas no BD do Postgre. Segue o código SQL das tabelas. Tabela Pessoa
-- Table: pessoa

-- DROP TABLE pessoa;

CREATE TABLE pessoa
(
  id bigint NOT NULL,
  nome character varying(255),
  telefone character varying(255),
  CONSTRAINT pessoa_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE pessoa OWNER TO postgres;
Tabela Paciente
-- Table: paciente

-- DROP TABLE paciente;

CREATE TABLE paciente
(
  ano character varying(255),
  curso character varying(255),
  pessoa_id_pessoa bigint NOT NULL,
  CONSTRAINT paciente_pkey PRIMARY KEY (pessoa_id_pessoa),
  CONSTRAINT fk308177915eb80448 FOREIGN KEY (pessoa_id_pessoa)
      REFERENCES pessoa (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE paciente OWNER TO postgres;

[color=red]Obrigada pessoal!![/color]
Até mais!
Dina

Criado 11 de maio de 2010
Respostas 0
Participantes 1