@Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "advert") public class Advert { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "广告唯一id") private Integer id; @ApiModelProperty(value = "广告名称") @NotBlank(message = "广告名称不可为空", groups = {AddGroup.class, UpdateGroup.class}) private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "position_id") private AdPosition position; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "media_id") private MediaFile media; } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "ad_position") public class AdPosition { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "位置名称") @NotBlank(message = "位置名称不可为空", groups = {AddGroup.class}) private String name; @OneToMany(mappedBy = "AdPosition", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "media_file") public class MediaFile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "媒体文件名称") private String fileName; @ApiModelProperty(value = "媒体文件类型") private Integer fileType; @ApiModelProperty(value = "媒体文件URL地址") private String fileUrl; @OneToMany(mappedBy = "MediaFile", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } public interface MyBaseMapper<T> extends Mapper<T>, MySqlMapper<T>,MyInsertListMapper<T> {} public interface AdvertMapper extends MyBaseMapper<Advert> {} @Autowired private AdvertMapper advertMapper; AdPosition adPosition = Optional.ofNullable(adPositionMapper.selectByPrimaryKey(advertDto.getPositionId())) .orElseThrow(() -> new RuntimeException("Ad position not found")); // 创建一个新的Advert对象 Advert advert = Advert.builder() .name("广告名称") .position(adPosition) // 假设AdPosition表中id为1的记录是需要的位置 .media(new MediaFile(1)) // 假设MediaFile表中id为1的记录是需要的媒体文件 .build(); // 调用insertSelective方法插入数据 advertMapper.insertSelective(advert);,报错Field 'position_id' doesn't have a default value ; Field 'position_id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'position_id' doesn't have a default value,这是为什么
时间: 2024-04-28 12:24:34 浏览: 93
这个错误是因为在 Advert 对象插入数据库时,position_id 字段为 null,但是该字段没有设置默认值,因此数据库会报错。解决方法是在 Advert 对象中设置一个有效的 AdPosition 对象,或者在 Advert 对象对应的表中设置 position_id 字段的默认值。如果你希望 position_id 不能为空,可以在 Advert 对象中使用 @NotNull 注解来限制该字段的值不能为空。另外,你在创建 Advert 对象时,使用了一个新的 MediaFile 对象,但是这个对象并没有在数据库中插入,因此可能会导致 MediaFile 对象缺失。建议在使用 MediaFile 对象时,先确认该对象是否存在于数据库中。
相关问题
@Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "advert") public class Advert { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "广告唯一id") private Integer id; @ApiModelProperty(value = "广告名称") @NotBlank(message = "广告名称不可为空", groups = {AddGroup.class, UpdateGroup.class}) private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "position_id") private AdPosition position; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "media_id") private MediaFile media; } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "ad_position") public class AdPosition { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "位置名称") @NotBlank(message = "位置名称不可为空", groups = {AddGroup.class}) private String name; @OneToMany(mappedBy = "AdPosition", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "media_file") public class MediaFile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "媒体文件名称") private String fileName; @ApiModelProperty(value = "媒体文件类型") private Integer fileType; @ApiModelProperty(value = "媒体文件URL地址") private String fileUrl; @OneToMany(mappedBy = "MediaFile", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } public interface MyBaseMapper<T> extends Mapper<T>, MySqlMapper<T>,MyInsertListMapper<T> {} public interface AdvertMapper extends MyBaseMapper<Advert> {},如何保存一个advert对象
保存一个`Advert`对象可以通过使用`AdvertMapper`中的方法来实现,具体步骤如下:
1. 创建一个`Advert`对象,并设置其属性值:
```
Advert advert = new Advert();
advert.setName("广告名称");
// 设置关联属性
AdPosition position = new AdPosition();
position.setId(1); // 假设要关联的AdPosition的id为1
advert.setPosition(position);
MediaFile media = new MediaFile();
media.setId(2); // 假设要关联的MediaFile的id为2
advert.setMedia(media);
```
2. 调用`AdvertMapper`中的保存方法进行保存:
```
@Autowired
private AdvertMapper advertMapper;
// 保存Advert对象
advertMapper.insert(advert);
```
在保存时,由于`AdPosition`和`MediaFile`实体类中的`adverts`属性都使用了`mappedBy`属性,因此不需要再手动设置关联关系的另一侧。
另外,如果需要同时保存多个`Advert`对象,可以将它们放在一个`List<Advert>`中,然后使用`MyInsertListMapper`中的`batchInsert`方法来进行批量保存,例如:
```
List<Advert> adverts = new ArrayList<>();
// 添加多个Advert对象到adverts列表中
advertMapper.batchInsert(adverts);
```
@Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "ad") public class Ad { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; @ManyToOne @JoinColumn(name = "position_id") private AdPosition position; @Column(name = "start_date") private LocalDateTime startDate; @Column(name = "end_date") private LocalDateTime endDate; private String link; }和@Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "ad_image") public class AdImage { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String url; @OneToOne(mappedBy = "image") private Ad ad; },为什么 public Ad add(@RequestBody AdDto adDto) { AdPosition position = adPositionRepository.getOne(adDto.getPositionId()); AdImage image = new AdImage(); image.setUrl(adDto.getImageUrl()); adImageRepository.save(image); Ad ad = new Ad(); ad.setName(adDto.getName()); ad.setPosition(position); ad.setStartDate(adDto.getStartDate()); ad.setEndDate(adDto.getEndDate()); ad.setLink(adDto.getLink()); ad.setImage(image); return adRepository.save(ad); }报错Cannot resolve method 'setImage' in 'Advert'
报错信息提示无法解析方法 'setImage',这是因为 Ad 类中没有名为 setImage 的方法。根据你提供的代码,Ad 类中只有一个名为 setImage 的属性,而不是方法。为了解决这个问题,你需要在 Ad 类中创建一个名为 setImage 的方法,该方法应该接收一个 AdImage 对象作为参数,并将其设置为 Ad 对象的 image 属性,例如:
```
public void setImage(AdImage image) {
this.image = image;
}
```
然后在 add 方法中,将 Ad 对象的 image 属性设置为你已经保存的 AdImage 对象,如下所示:
```
Ad ad = new Ad();
ad.setName(adDto.getName());
ad.setPosition(position);
ad.setStartDate(adDto.getStartDate());
ad.setEndDate(adDto.getEndDate());
ad.setLink(adDto.getLink());
AdImage image = new AdImage();
image.setUrl(adDto.getImageUrl());
adImageRepository.save(image);
ad.setImage(image);
return adRepository.save(ad);
```
这样就能够成功将 AdImage 对象和 Ad 对象关联起来了。
阅读全文