尝试将正确的关系数据添加到一对多联接的另一侧时,我遇到了此异常。
由于某种原因,它没有自动获取 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);
慕容3067478
慕的地6264312
相关分类