有2个对象
@Entity
@Table(name = "real_estate", schema="data1c")
@Proxy(lazy = false)
public class RealEstate {
@Id
@Column(name = "id")
UUID id;
@Column(name = "name")
String name;
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinColumn(name="city", nullable = false)
private City city;
@Column(name = "website")
String website;
@Column(name = "logo")
String logo;
@Column(name = "logo_web")
String logoWeb;
@Column(name = "foto")
String foto;
@Column(name = "foto_web")
String fotoWeb;
@Column(name = "longitude")
Double longitude;
@Column(name = "latitude")
Double latitude;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public String getLogoWeb() {
return logoWeb;
}
public void setLogoWeb(String logoWeb) {
this.logoWeb = logoWeb;
}
public String getFoto() {
return foto;
}
public void setFoto(String foto) {
this.foto = foto;
}
public String getFotoWeb() {
return fotoWeb;
}
public void setFotoWeb(String fotoWeb) {
this.fotoWeb = fotoWeb;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
@Override
public String toString() {
return "RealEstate{" +
"id=" + id +
", name='" + name + '\'' +
", city=" + city +
", website='" + website + '\'' +
", logo='" + logo + '\'' +
", logoWeb='" + logoWeb + '\'' +
", foto='" + foto + '\'' +
", fotoWeb='" + fotoWeb + '\'' +
", longitude=" + longitude +
", latitude=" + latitude +
'}';
}
}
城市对象:
@Entity
@Table(name = "city", schema="data1c")
@Proxy(lazy = false)
public class City {
@Id
@Column(name = "id")
private UUID id;
@Column(name = "name")
private String name;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "City{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
我需要从 api 中获取数据并将其上传到数据库。
一个示例 json 是这样的:
{
"id": "c67ec057-ea71-11e5-b9a5-b4b52f5405e7",
"name": "Проект очереди 1",
"city": "a4874e7c-2906-11e9-80d9-00155d101f25",
"website": "",
"logo": "",
"logoWeb": "",
"foto": "",
"fotoWeb": "",
"longitude": 0,
"latitude": 0
}
问题是 city 不是作为一个对象出现的,而是作为一个 id(标识符)出现的。我如何序列化?帮助
我正在尝试这样的事情:
public RealEstate serializerealrest() throws IOException {
String body = "{\n" +
" \"id\": \"c67ec057-ea71-11e5-b9a5-b4b52f5405e7\",\n" +
" \"name\": \"Проект очереди 1\",\n" +
" \"city\": \"a4874e7c-2906-11e9-80d9-00155d101f25\",\n" +
" \"logo\": \"\",\n" +
" \"logoWeb\": \"\",\n" +
" \"foto\": \"\",\n" +
" \"fotoWeb\": \"\",\n" +
" \"longitude\": 0,\n" +
" \"latitude\": 0\n" +
" }";
RealEstate realEstate = mapper.readValue(body, new TypeReference<RealEstate>(){});
return realEstate;
}
有很多选择。例如,写下特殊的注释就足够了,这些注释将告诉序列化程序要解析什么以及在哪里解析。对于城市的 id,我们将添加一个单独的字段,我们不会将其保存在数据库中,但我们会将其显示给我们的序列化程序。发现龙目岛,你能写多长时间 gettras、setter 和其他废话?gradle 的依赖项:compileOnly 'org.projectlombok:lombok:1.18.8'
看起来像这样