@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'
时间: 2023-11-22 10:54:47 浏览: 67
报错信息提示无法解析方法 '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 对象关联起来了。
相关问题
@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表时,将关联的其余两张表的数据一并查询出来?请将mapper.xml文件全部展示出来
AdvertMapper.xml文件如下:
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.AdvertMapper">
<resultMap id="advertMap" type="com.example.entity.Advert">
<id property="id" column="id" />
<result property="name" column="name" />
<association property="position" column="position_id" javaType="com.example.entity.AdPosition">
<id property="id" column="id" />
<result property="name" column="name" />
</association>
<association property="media" column="media_id" javaType="com.example.entity.MediaFile">
<id property="id" column="id" />
<result property="fileName" column="file_name" />
<result property="fileType" column="file_type" />
<result property="fileUrl" column="file_url" />
</association>
</resultMap>
<select id="selectAdvert" resultMap="advertMap">
SELECT a.id, a.name, a.position_id, a.media_id, p.name as position_name, m.file_name, m.file_type, m.file_url
FROM advert a
LEFT JOIN ad_position p ON a.position_id = p.id
LEFT JOIN media_file m ON a.media_id = m.id
WHERE a.id = #{id}
</select>
</mapper>
```
在上面的XML文件中,我们定义了一个名为advertMap的结果映射,它将advert表、ad_position表和media_file表的数据全部查询出来。我们还定义了一个名为selectAdvert的查询语句,它使用advertMap作为结果映射,并在SELECT语句中使用LEFT JOIN将三个表连接起来。最后,我们将这个查询语句的ID设置为selectAdvert,以便在Java代码中调用它。
Warn: Could not find @TableId in Class: com.joyuai.ticket.comm.product.entity.suppgoods.pass.SuppGoodsPriceLevelPass
这个警告信息通常出现在Spring Data JPA的上下文中,它表示在com.joyuai.ticket.comm.product.entity.suppgoods.pass.SuppGoodsPriceLevelPass这个类中找不到@TableId注解,这通常是用于标记该实体类作为数据库表主键的字段。
`@TableId`注解在JPA中用来标识哪些字段应该关联到数据库的主键,并且会被自动管理。如果想要解决这个问题,你需要找到该类中的主键字段,然后添加`@TableId`注解,例如:
```java
@Entity
@Table(name = "supp_goods_price_level_pass")
public class SuppGoodsPriceLevelPass {
// 确定并添加@TableId注解到你的主键字段上
@TableId @GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
// 其他字段...
}
```
这里的`Long id`假设`id`是主键字段,并且采用了默认的生成策略`GenerationType.IDENTITY`(如果是数据库自增ID)。记得将实际的字段类型和生成策略替换为你类的真实情况。
阅读全文