Spring Modelling JPA Person -> Personal Modelling -> Function

Asked

Viewed 592 times

1

I did the data modeling (UML) but at the time of implementing I’m doubtful how to do.

I wish there was only one table Person.

In that table person I would have the field tipoPessoa("p","f") and fields if the person is customer, barber, attendant, supplier, manufacturer, etc.

I wanted to make this implementation in Spring JPA that I saw that is very easy to use.

Here’s basically how I want the table on the bench:

Aqui vai basicamente como quero que fique a table no banco:

Here’s how I want it to stay in java:

Aqui é como quero que fique no java:

I know that this relationship between people and types could be manytomany generating an extra table, but for this project I want to do it anyway.

The doubt is how I do pro Spring understand that I want everything to be in the same table with Ex fields: pess.forn = "f", "t"

  • 3

    Regardless of the technology used it is not a good idea to model the database other than memory. I don’t like any of the models. This model is stuck in real life.

2 answers

2

You can use the strategy of JOIN Single Table, where Hibernate creates a single table for each hierarchy. This is also the default strategy chosen by JPA if you do not explicitly specify one.

For didactic purposes, I will show a definite example of this strategy explicitly:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="tipoPessoa", 
    discriminatorType = DiscriminatorType.STRING)
public class Pessoa {
    @Id
    private long idPessoa;
    private String contato;
    // demais campos
}

Above I created a table Pessoa and said that to differentiate between different people (legal and physical) I will use the field tipoPessoa. The information that goes on tipoPessoa is defined in child classes by the annotation @DiscriminatorValue. See below:

@Entity
@DiscriminatorValue("pf")
public class PessoaFisica extends Pessoa {
    private String cpf;
    // demais campos
}

@Entity
@DiscriminatorValue("pj")
public class PessoaJuridica extends Pessoa {
    private String cnpj;
    // demais campos
}

@Entity
@DiscriminatorValue("cliente")
public class Cliente extends PessoaFisica {
    // demais campos do cliente
}
  • and how would be the part of the columns to differentiate whether it is customer, employee, supplier(pess.Forn = 'F') ?

  • @Brunobrito, you can extend the PessoaFisica in another entity and add a @DiscriminatorValue for each entity Cliente, Funcionario , etc. I have not tested but I think it works! I edited my question with this suggestion.

-1

You can create a pess table as follows:

create table pess(
id NUMBER not null,
email              VARCHAR2(100),
forn VARCHAR2(1) default 'f')

OBS: the above script has as an example ORACLE.

The Person class:

public class Person{
 private Long id;
 private String email;
 private String forn;
}

When you go to do the Inserts you apply your logic to insert only values of type 'f' or 'j'. You can use the flyway to read your sql scripts. If you don’t know flaway, I recommend q vc search on. It will give you a freedom to create your tables.

Browser other questions tagged

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