Problem with table extension in Postgresql, Person and Physical Person

2

I'm making an application that has a people register, it's a Person CRUD that can be both physical and legal. the problem is that in my design a Pessoa and a Pessoa Fisica should be created in the database, but it is creating only one person.

I have a Pessoa entity.

package entys;

import java.io.Serializable;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;

@Entity @Table (name="person", schema="dremcom_drem") public class Person implements Serializable {     private static final long serialVersionUID = 1L;

@Id  
@GeneratedValue(strategy=GenerationType.SEQUENCE)  
@Column(name="id")    
private Long id;
private String endereco;
private String cep;


public Long getId() {  
    return this.id;  
}
private String nome;


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

public Pessoa() {
    super();
}

public String getNome() {
    return nome;
}

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

public String getEndereco() {
    return endereco;
}

public void setEndereco(String endereco) {
    this.endereco = endereco;
}

public String getCep() {
    return cep;
}

public void setCep(String cep) {
    this.cep = cep;
}

}

Soon after I have an entity of Pessoa Fisica

package entys;

import java.io.Serializable;

import javax.persistence.Entity; import javax.persistence.Table;

@Entity @Table (name="personal", schema="dremcom_drem") public class PersonFisica extends Person implements Serializable {

private static final long serialVersionUID = 1L;
private String cpf;
private String matricula;
private String rg;

public String getCpf() {
    return cpf;
}
public void setCpf(String cpf) {
    this.cpf = cpf;
}
public String getMatricula() {
    return matricula;
}
public void setMatricula(String matricula) {
    this.matricula = matricula;
}
public String getRg() {
    return rg;
}
public void setRg(String rg) {
    this.rg = rg;
}

} so I'm doing a test to insert an individual.

package testeUnitario;

import javax.persistence.EntityManager;

import util.JPAUtil; import entys.Pessoa Fisica;

public class TestPaper {

public static void main(String[] args) {

    PessoaFisica pessoaFisica = new PessoaFisica();

    pessoaFisica.setNome("Andrades");
    pessoaFisica.setEndereco("67 sul");
    pessoaFisica.setCep("7700000");
    pessoaFisica.setCpf("89076");
    pessoaFisica.setMatricula("fc2012");
    pessoaFisica.setRg("89765");

    EntityManager em = JPAUtil.getEntityManager();
    em.getTransaction().begin();
    em.persist(pessoaFisica);
    em.getTransaction().commit();
    em.close();
}

}

What happens is that when I enter my bank it creates only one person and does not create an individual and only places those fields in person

    
asked by anonymous 20.04.2014 / 03:32

1 answer

2

As a rule (of course there may be exceptions), I would recommend giving preference to using composition rather than inheritance and using the inheritance strategy of one class for each table (Table per class).

In doing so, I believe that the application code is easier to understand, evolve and maintain.

In any case, in your specific example, the "error" of inserting only a person and not an individual must be because you have not configured the annotation in your parent class (Person)

@Inheritance(strategy = InheritanceType.JOINED) 

Useful references:

20.04.2014 / 03:52