Use of only one attribute of an entity

Asked

Viewed 53 times

3

I’m building a Rest API for Cities and Customers registration. My problem would be in relation to the Customer class that has as attributes personal data of the customer and a City, that would be the city of residence related to the entity City.

The entity City:

package com.desafio.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;


@Data
@Entity
@Table(name = "cidade")
public class Cidade {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "cidade", nullable = false)
    private String nomeCidade;
    @Column(name = "estado", nullable = false)
    private String estado;

}

The entity Client:

package com.desafio.model;

import java.time.LocalDate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import lombok.Data;

@Data
@Entity
@Table(name = "cliente")
public class Cliente {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "nome_cliente")
    private String nomeCliente;
    @Column(name = "sexo")
    private String sexo;
    @Column(name = "data_nascimento")
    private LocalDate dataNascimento;
    @Column(name = "idade")
    private Integer idade;

    @ManyToOne
    private Cidade cidade;

}

The main problem is that, at the moment the Customer will make the registration, then generate a POST, in the field where should have only the name of the city, requests the entire City object, including the ID. The expected Json is as follows:

{
  "cidade": {
    "estado": "string",
    "id": 0,
    "nomeCidade": "string"
  },
  "dataNascimento": "string",
  "id": 0,
  "idade": 0,
  "nomeCliente": "string",
  "sexo": "string"
}

I would like to know what can be done, so that I have a relationship between the two entities, but capturing only the name of the city for the Client class.

1 answer

4


Separate responsibilities between the domains of your controller and database.

I answered a question similar to this one here a time ago that encompasses its dilemma, specifically in this passage:

To separate the responsibility of the bank entity being the same to be requested/returned to the client, containing both persistence-related annotations and parse-related annotations (@Table and @Jsonignore for example).

For your case, you have the need to customize the request object sent by your client, and are using your database entity for this. Over time, the maintenance of your code will become more complex, since your API will evolve, and with that more data will be included in the request, validations must be carried out...

A solution would be to adopt the creation of an object of Request and Response for your operations, with this you would gain the flexibility to own a class with only the data that interests you in this request (eliminating fields and structures, or adding fields and validations)and there is no risk that your request/response properties will change when there is any change in your database.

Browser other questions tagged

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