Insert/get multiple tables without Id

Asked

Viewed 70 times

0

Good afternoon guys, I’m having 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 assembled from Hibernate classes using JPA. In the modeling one has the street code, street the neighborhood code and goes going until arriving in parents. In the Relationship between these entities I used the annotation @ManyToOne(cascade = CascadeType.MERGE) to insert into the bank if it does not exist and if it exists it does not insert anything and just take the ID of the already inserted. That’s the problem, I can insert normally, but it creates a line for each person, but I want to insert only what is new, example:

I want that 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 already has and continue the process doing the same procedure for the other entities below this (Already has the state picks up the id at the base, already has city picks up the id at the base,has no neighborhood inserts the line placing the id of the city, has no street inserts putting the id of the neighborhood). With JDBC I did it easily using selects, but I believe that in Hibernate/JPA I can do it more easily.

In this case, I get from the user the address data filled, but without the Ids, so I would have to do this search in the database by the address string and not by the id,

Follows 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*/ 
}

Bairro 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 in the bank I give a entityManager.merge(Funcionario);

And the procedure of @ManyToOne(cascade = CascadeType.MERGE) go to the last class that would be Parents. You can 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}}
  • All entities are being duplicated or only Pais? You have an example code showing how you are searching for entities, associating with the employee and updating them?

  • You want Hibernate to automatically search records based on the given values and persist with existing records or, if not, create records?

No answers

Browser other questions tagged

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