首页 > 解决方案 > 为什么不在具有多对一关系的表中记录一个 id?弹簧数据,Liquidbase

问题描述

我无法弄清楚 id 的记录可能存在什么问题。本质上:我创建对象“建筑物” - 我在其中添加一个对象“照片”,因此我想在数据库中记录一个对象建筑物(其中已经有一个照片对象)。正在记录建筑物,照片也在那里,但是建筑物的ID没有保存在“照片”表中。为什么会这样?

建筑表 建筑表:照片表

照片表

@Entity
public class Building {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String description;
    @OneToMany(mappedBy = "building", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Photo> photos;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "category_building",
            joinColumns = {@JoinColumn(name = "building_id")},
            inverseJoinColumns = {@JoinColumn(name = "category_id")
            })
    private List<Category> categories;

    public Building() {
    }

    public Building(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public Building(String name, String description, List<Photo> photos) {
        this.name = name;
        this.description = description;
        this.photos = photos;
    }

    public Building(String name, String description, List<Photo> photos, List<Category> categories) {
        this.name = name;
        this.description = description;
        this.photos = photos;
        this.categories = categories;
    }
}

@Entity
public class Photo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "building_id")
    private Building building;
    @Column(name = "original_size")
    byte[] originalSize;
    @Column(name = "small_size")
    byte[] smallSize;
    @Column(name = "middle_size")
    byte[] middleSize;
    @Column(name = "large_size")
    byte[] largeSize;

    public Photo() {
    }

    public Photo(Building building, byte[] originalSize) {
        this.building = building;
        this.originalSize = originalSize;
    }

标签: javamysqlspring-data-jpaspring-dataliquibase

解决方案


推荐阅读