我有以下模型,它們具有一對一、一對多和多對一的關系。大多數模型都很好,除了一個。
我的json資料:
{
"name": "Warehouse A",
"location": {
"lat": "47.13111",
"long": "-61.54801"
},
"cars": {
"location": "West wing",
"vehicles": [
{
"make": "Volkswagen",
"model": "Jetta III",
"year_model": 1995,
"price": 12947.52,
"licensed": true,
"date_added": "2018-09-18"
},
{
"make": "Chevrolet",
"model": "Corvette",
"year_model": 2004,
"price": 20019.64,
"licensed": true,
"date_added": "2018-01-27"
}
]
}
}
Java模型:
@Getter
@Setter
@NoArgsConstructor
@Data
@Entity
@Table(name = "warehouse")
public class Warehouse {
@JsonProperty("_id")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@JsonProperty("name")
private String name;
@JsonProperty("location")
@OneToOne(mappedBy = "warehouse", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Location location;
@JsonProperty("cars")
@OneToOne(mappedBy = "warehouse", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Car cars;
}
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "car")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String location;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "warehouse_id", referencedColumnName = "id")
private Warehouse warehouse;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToMany(mappedBy = "car", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Vehicle> vehicles = new ArrayList<>();
}
@Getter
@Setter
@NoArgsConstructor
@Entity
public class Vehicle {
@JsonProperty("_id")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@JsonProperty("make")
private String mark;
private String model;
@JsonProperty("year_model")
private int year;
private double price;
private boolean licensed;
@JsonProperty("date_added")
private Date dateAdded;
@NotNull
@JoinColumn(name = "car_id")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Car car;
}
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "location")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private float lat;
@JsonProperty("long")
private float lon;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "warehouse_id", referencedColumnName = "id")
private Warehouse warehouse;
}
在 H2 DB 中插入資料后,我可以看到流動的表和資料。

車:

車輛:

地點:

如您所見,除了位置之外,所有資料都很好。它缺少warehouse_id。我錯過了什么?倉庫和汽車之間的相同代碼和表關系也可以一對一地作業。但對于位置,它不起作用。我不知道錯過了什么。
uj5u.com熱心網友回復:
感謝pringi提出問題。在pringi的問題之后,我檢查了我的保存方法:-
public Warehouse saveWareHouse(Warehouse warehouse) {
Car car = warehouse.getCars();
car.setWarehouse(warehouse);
Location location = warehouse.getLocation(); // is added newly
location.setWarehouse(warehouse); // is added newly
List<Vehicle> vehicles = car.getVehicles();
vehicles.forEach( vehicle -> {
vehicle.setCar(car);
});
return warehouseRepository.save(warehouse);
}
添加缺少的 2 行后,它已修復且作業正常。

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/406477.html
標籤:
上一篇:如何檢索此Hibernate@ManyToOne關系中記錄的所有子項?
下一篇:當我可以使用java在HibernateJPA中通過等效int值進行查詢時,為什么我不能通過String值進行查詢
