How to prevent Hibernate from creating columns from the fields of an abstract mother class

Asked

Viewed 82 times

0

I have an abstract class Pessoas with methods getters and setters and a concrete class that inherits from Pessoas (members). By mapping the concrete class as Entity and its fields, it is creating the columns that refer to the abstract class Pessoas in the database. How to fix this? I already have a specific table for people and need another one specific for members.

Abstract class:

@MappedSuperclass
public abstract class Pessoas {

@Column
protected String nome;

@Column
protected String identidade;

@Column
protected String cpf;

@Column
protected String data_cadastro;

@Column
protected String naturalidade;

@Column
protected String nacionalidade;

@Column(name="data_nascimento")
protected String dataNascimento;

@Column(name="genero")
protected String sexo;

@Column(name = "estado_civil")
protected String estadoCivil;

@Column
protected String conjuge;

@Column(name = "data_casamento")
protected String dataCasamento;

@Column
protected String escolaridade;

@Column(name = "nome_pai")
protected String nomePai;

@Column(name = "nome_mae")
protected String nomeMae;

@Column(name = "data_batismo")
protected String dataBatismo;

@Column
protected String situacao;

@Column
protected String endereco;

@Column
protected String numero;

@Column
protected String bairro;

@Column
protected String cidadeEndereco;

@Column
protected String estadoEndereco;

@Column
protected String celular;

@Column
protected String telefone;

@Column
protected String email;

//Getter e Setters

Concrete class:

@Entity
@Table(name = "tbl_pessoas")
public class PessoasModel extends Pessoas {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id_pessoa;

public int getId_pessoa() {
    return id_pessoa;
}

public void setId_pessoa(int id_pessoa) {
    this.id_pessoa = id_pessoa;
}

1 answer

0

You can make use of the @Inheritance annotation in your superclass.

Example:

import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Table(name = "PESSOA")
@Inheritance(strategy = InheritanceType.JOINED) 
public abstract class Pessoa {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    ...
}


@Entity
@Table(name = "MEMBRO")
public class Membro extends Pessoa{
    ...
} 

In this case you have a table for Person, only with the attribute fields defined in the class itself ,and a table for Member, only with the attributes defined in the class idem itself. These will be joined by the id defined in the person class (JOINED).

You can explore the other two Enum## Inheritance Type headers to solve other cases you need:

public enum InheritanceType { 

    /** A single table per class hierarchy. */
    SINGLE_TABLE, 

    /** A table per concrete entity class. */
    TABLE_PER_CLASS, 

    /** 
     * A strategy in which fields that are specific to a 
     * subclass are mapped to a separate table than the fields 
     * that are common to the parent class, and a join is 
     * performed to instantiate the subclass.
     */
    JOINED 
}

Reference Baeldung

  • 1

    Thanks man... I’ll give a study

  • Test with the first example in your application and put here the doubts.

Browser other questions tagged

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