java—为什么不在具有多对一关系的表中记录id?spring数据,liquidbase

798qvoo8  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(164)

我想不出记录身份证有什么问题。本质是:我创建了对象“building”——我在其中添加了一个对象“photo”,结果我想在数据库中记录一个对象一个建筑(其中已经有一个photo对象)。建筑物正在被记录,照片也在那里,但是建筑物的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;
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题