java - 为什么不在具有多对一关系的表中记录一个 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;
}
解决方案
推荐阅读
- python - 如何使用 Databricks Python 在 Jinja2 中添加 javascript 和 css 文件?
- python - 数据框中3列的最大值?
- java - 退出第一种方法后如何停止执行
- java - 在 Java 中使用 MulticastSocket 关闭发送到发件人
- sql-server - 尽管凭据正确,但 Azure SQL Server 连接错误 - 错误号:18456,状态:1,类:14
- javascript - 测试 onChange 处理程序时,React State 变得未定义
- firebase - 如何在firebase中动态创建cron作业
- spring-boot - SpringBootTest时如何忽略ContextRefreshedEvent?
- java - 可选的 map 和 orElse 必须返回相同的类型
- r - 在 R 中制作具有多列的数据透视表