I have following models they have one-to-one and one-to-many and many-to-one relationship. Most of the model are fine except one.
My json data:
{ "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 models:
@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; }
After inserting data in H2 DB, I can see flowing tables and data.
Car:
Vehicle:
Location:
As you can see all data is fine except Location. It has missing warehouse_id. What did I miss? Also identical code and table relationship between warehouse and car one-to-one works fine. But for location it doesn’t work. I have no idea what was missed.
Advertisement
Answer
Thanks to pringi for asking the question. After pringi’s question I checked my saving method:-
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); }
After adding missing 2 line, it is fixed and working fine.