Problem with table extension in Postgresql, Person and Physical Person

Asked

Viewed 291 times

2

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

I have an entity Pessoa.

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 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 Personal extends Person Serializable Content {

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 taking a test to insert a physical person.

package testeUnitario;

import javax.persistence.Entitymanager;

import util.Jpautil; import entys.Personal physique;

public class Testepessoafisica {

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 insert in my bank it creates only one person and does not create a physical person and just puts those fields in person

1 answer

2


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

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

In any case, in your specific example, the "mistake" of inserting only one person and not an individual should be because you have not configured Annotation in your mother class (Person)

@Inheritance(strategy = InheritanceType.JOINED) 

Useful references:

  • Marcelo, I understood the concept and why to use composition instead of heritage, but specifically in this project I need to use inheritance because I will have to submit for a DBA that likes heritage... but I have seen that he is wrong about deeper consultations. a question where I put the Inheritance(Strategy = Inheritancetype.JOINED) in Pessoa? another question I could not leave the person as an abstract class? so it would generate only personal and personal in the bank?

  • the Inheritance Annotation you could put up / before the Entity Annotation

  • yes, you could leave the person as an abstract class, but you need to test and discover on your own the effects of this on your application both in terms of persistence and in terms of business rule. For example, what if your system allows you to register a person and only then configure or detail whether it is physical or legal? This you and DBA need to define, in view of the business requirements of the application being developed.

  • understand thanks for the tips once again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.