Spring 引导休眠复杂 table 关系部分起作用
Spring boot hibernate complex table relationship works partially
我有以下模型,它们具有一对一、一对多和多对一的关系。大多数模型都很好,除了一个。
我的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中插入数据后,我可以看到流动的tables和数据。
汽车:
车辆:
地点:
如您所见,除位置外,所有数据均正常。它缺少 warehouse_id。我错过了什么?同样的代码和 table 仓库和汽车一对一的关系也可以正常工作。但是对于位置它不起作用。我不知道错过了什么。
感谢 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 行后,它已修复并且工作正常。
我有以下模型,它们具有一对一、一对多和多对一的关系。大多数模型都很好,除了一个。
我的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中插入数据后,我可以看到流动的tables和数据。
汽车:
车辆:
地点:
如您所见,除位置外,所有数据均正常。它缺少 warehouse_id。我错过了什么?同样的代码和 table 仓库和汽车一对一的关系也可以正常工作。但是对于位置它不起作用。我不知道错过了什么。
感谢 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 行后,它已修复并且工作正常。