这个问题是由延迟加载引起的。在任务的本质中,有一个名为“resultImage”的字段。此字段包含一个大图片,当卸载所有任务对象时,我不想卸载它。Map程序将Task对象转换为DTO对象时发生错误。
错误日志;
enter image description here
任务实体:
@Entity
@Table(name = "TASKS", schema = "main")
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "USER_ID", referencedColumnName = "id")
private User user;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "RESULT_IMAGE_ID", referencedColumnName = "id")
private Image resultImage;
@OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "PREVIEW_RESULT_IMAGE_ID", referencedColumnName = "id")
private Image previewResultImage;
}
图像实体:
@Entity
@Table(name = "IMAGES",schema = "main")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Image {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Column(name = "DATA")
@Lob
private byte[] data;
}
DTO级:
@Data
@Builder
public class ResponseDTO {
private Long taskId;
private String sampleImageData;
private String imagePreviewResultData;
}
发生错误的方法:
public List<ResponseDTO> getAllTasks(Long userId) {
List<Task> tasks = taskRepository.findAllByUserId(userId);
return tasks.stream().
map(Mapper.INSTANCE::taskToResponseDTO).
collect(Collectors.toList());
}
Map器中的变换方法:
@org.mapstruct.Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface Mapper {
Mapper INSTANCE = Mappers.getMapper(Mapper.class);
@Mapping(target = "taskId", source = "id")
@Mapping(target = "sampleImageData", source = "imageMetadata.sampleImage.data",qualifiedByName = "dataToBase64")
@Mapping(target = "imagePreviewResultData", source = "previewResultImage.data",qualifiedByName = "dataToBase64")
ResponseDTO taskToResponseDTO(Task task);
}
我尝试禁用未配置字段的Map,但没有帮助。
我会很高兴任何帮助!
1条答案
按热度按时间f3temu5u1#
好了,在查看了您的异常跟踪(您应该已经添加为文本)之后
问题是Jackson序列化库不知道如何处理Hibernate代理(在延迟加载的情况下使用)
因此,您需要遵循错误日志中的建议:
从Jackson序列化配置中禁用功能
FAIL_ON_EMPTY_BEANS
。