【问题标题】:Spring boot hibernate complex table relationship works partiallySpring Boot Hibernate 复杂表关系部分工作
【发布时间】:2022-01-06 11:32:54
【问题描述】:

我有以下模型,它们具有一对一、一对多和多对一的关系。大多数模型都很好,除了一个。

我的 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。我错过了什么?仓库和汽车之间的相同代码和表关系也可以一对一地工作。但对于位置,它不起作用。我不知道错过了什么。

【问题讨论】:

  • 你是如何保存数据的?可以出示一下代码吗?
  • @pringi 非常感谢!你的问题帮助我解决我的问题。我发布了固定保存代码作为答案。

标签: java spring-boot hibernate


【解决方案1】:

感谢 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 行后,它已修复并且工作正常。

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2017-03-07
    • 1970-01-01
    • 2018-07-11
    • 2021-06-15
    • 2021-03-04
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多