@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; } ,但是方法 @PostMapping 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); } 中的ad.setImage(image);报错Cannot resolve method 'setimage' in 'Advert',请说明原因和解决方案
时间: 2023-11-22 09:54:55 浏览: 180
Spring Boot JPA中使用@Entity和@Table的实现
5星 · 资源好评率100%
出现该错误的原因是Ad类中没有定义名为setImage的方法,因为在Ad类中定义的是一个名为setImage的方法,而不是setimage。因此,应该将ad.setImage(image)更改为ad.setImage(image),即将image的首字母改为大写。
修改后的代码如下:
```
@PostMapping
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);
}
```
阅读全文