Insert / get multiple tables without Id

0

Good afternoon guys, I have a problem affecting me a few days already. The situation is as follows, I have a simple relationship: There are tables person, street, neighborhood, city, state, parents all mounted from Hibernate classes using JPA. In modeling the person has the street code, street the code of the neighborhood and goes going until arriving in parents. In the Relationship between these entities I used the @ManyToOne(cascade = CascadeType.MERGE) annotation to insert in the database if it does not exist and if it exists it does not insert anything and just get the ID of the inserted ja. This is the problem, I can enter normally, but it creates a line for each person, but I want to insert only what is new, eg:

I want if it already exists, for example, "Brazil" in the Country, it does not insert a new duplicate record in the Country table, but rather take the id of what it already has and continue the process by doing the same procedure for the other entities below Of this (already has the state takes the id in the base, already has city picks the id in the base, has no neighborhood inserts the line putting the id of the city, has no street inserts putting the id of the neighborhood). With JDBC I've done this easily using selects, but I believe that in Hibernate / JPA I can do it more easily.

In this case, I get the user's address data filled in, but without the Ids, so I would have to search the database for the address string instead of the id,

Follow the code:

@Entity(name = "FUNCIONARIO")
public class Funcionario implements Serializable
{

 /*Variaveis*/

 @ManyToOne(cascade = CascadeType.MERGE)
 @JoinColumn
 @Valid
 private Rua rua;
 @ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn
 private Departamento departamento;

 /*ToString() + Getters e Setters*/ 
}

Street Class:

@Entity(name = "RUA")
public class Rua implements Serializable
{
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long codRua;
  @NotEmpty
  private String nomeRua;
  @NotEmpty
  private String cep;
  @ManyToOne(cascade = CascadeType.MERGE)
  @JoinColumn
  @Valid
  private Bairro bairro;

 /*ToString() + Getters e Setters*/ 
}

Neighborhood Class:

@Entity(name = "BAIRRO")
public class Bairro implements Serializable
{
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long codBairro;
  @NotEmpty
  private String nomeBairro;
  @ManyToOne(cascade = CascadeType.MERGE)
  @JoinColumn
  @Valid
  private Cidade cidade;

  @OneToMany(mappedBy = "bairro", targetEntity = Rua.class, 
      fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  private List<Rua> ruas;

 /*ToString() + Getters e Setters*/ 
}

Insertion into the database I give entityManager.merge(Funcionario);

And the procedure of @ManyToOne(cascade = CascadeType.MERGE) follows the last class that would be Pais. Can you give me a light on how to proceed?

Edit 1:

This is the object I get from the user:

Funcionario{codFuncionario=3, nomeCompleto=Jhonatan Colina, dataNascimento=Tue Apr 30 00:00:00 BRT 1996, cpf=212.132.312-13, rg=21.321.213-2, orgaoEmissor=SSP-sp, [email protected], [email protected], telefone=(23) 12132-3121, numeroCasa=0000, situacao=true, observacoes=, rua=Rua{codRua=null, nomeRua=Avenida Prefeito Ângelo Celeguim, cep=07809000, bairro=Bairro{codBairro=null, nomeBairro=Vila Santista, cidade=Cidade{codCidade=null, nomeCidade=Franco da Rocha, estado=Estado{codEstado=null, nomeEstado=SP, pais=Pais{codPais=null, nomePais=Brasil}}}}}, departamento=Departamento{codDepartamento=1, nomeDepartamento=null, telefone=null, telefoneComercial=null, situacao=false, secretaria=null}}
    
asked by anonymous 24.07.2018 / 21:19

0 answers