尝试将正确的关系数据添加到一对多联接的另一侧时,我遇到了此异常。由于某种原因,它没有自动获取 car_id这是我的代码。汽车模型@Data@Entity@Table(uniqueConstraints={ @UniqueConstraint(columnNames = {"date", "auctionNumber", "auctionId"})})public class Car { @Id @GeneratedValue private UUID id; private String date; private int auctionId; private int auctionNumber; @OneToMany(mappedBy = "car", orphanRemoval = true, cascade={CascadeType.ALL}) private List<CarPhoto> photos; public void addPhoto(CarPhoto photo) { if (photos == null) { photos = new ArrayList<CarPhoto>(); } photos.add(photo); photo.setCar(this); }}汽车照片模型@Data@Entity@Table(name="car_photo")public class CarPhoto { @Id @GeneratedValue private UUID id; @Column(unique=true) private String path; @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="car_id", insertable=false, updatable=false) private Car car;}Repository @Repository 公共接口 CarRepository 扩展 CrudRepository { }持久性...loop cardata { Car car = new Car(); car.setAuctionId(cardata.auctionId); car.setAuctionNumber(cardata.auctionNumber); car.setDate(cardata.date); Pattern.compile("#").splitAsStream(cardata.pictures).forEach(path - > { CarPhoto photo = new CarPhoto(); photo.setPath(path); car.addPhoto(photo); }); cars.add(car);}carRepository.saveAll(cars);
2 回答
慕容3067478
TA贡献1773条经验 获得超3个赞
实际上,您需要从映射中删除insertable = false
和删除:updatable = false
@JoinColumn(name="car_id", insertable=false, updatable=false)
并覆盖equals
和实体的hashCode
方法以避免重复实体。您可以使用 lombok 的.Car
CarPhoto
@EqualsAndHashCode(callSuper = false)
因为只有insertable=true
您才能插入实体,但这可能违反唯一约束。
慕的地6264312
TA贡献1817条经验 获得超6个赞
insertable=false
从您的声明中删除@JoinColumn(name="car_id", insertable=false, updatable=false)
。因为现在您拒绝设置和更新字段car_id
添加回答
举报
0/150
提交
取消