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.