Код: Выделить всё
2024-06-09T23:20:44.470+03:00 WARN 5036 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42804
2024-06-09T23:20:44.470+03:00 ERROR 5036 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: column "bytes" is of type bytea but expression is of type bigint
Hint: You will need to rewrite or cast the expression.
Position: 56
2024-06-09T23:20:44.479+03:00 ERROR 5036 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement [ERROR: column "bytes" is of type bytea but expression is of type bigint
Hint: You will need to rewrite or cast the expression.
Position: 56] [insert into image (bytes,name,time_added,type) values (?,?,?,?)]; SQL [insert into image (bytes,name,time_added,type) values (?,?,?,?)]] with root cause
org.postgresql.util.PSQLException: ERROR: column "bytes" is of type bytea but expression is of type bigint
Hint: You will need to rewrite or cast the expression.
Код: Выделить всё
@Entity
@Getter
@Setter
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class Image {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
@Lob
@Column(name = "bytes", columnDefinition = "bytea")
private byte[] data;
@OneToOne(mappedBy = "image")
private Baby baby;
private LocalDateTime timeAdded;
}
Код: Выделить всё
@PostMapping(path = "/{babyId}")
public ResponseEntity saveBaby(@PathVariable Long babyId ,@RequestParam("image") MultipartFile image, @RequestHeader("Authorization") String token) throws IOException {
return imageService.saveImages(babyId, image, token);
}
Код: Выделить всё
@Service
@AllArgsConstructor
public class ImageService {
private static final Logger LOGGER = LoggerFactory.getLogger(ImageService.class);
private ImageRepository imageRepository;
private BabyService babyService;
@Transactional
public ResponseEntity saveImages(Long babyId, MultipartFile files, String token) throws IOException {
Optional babyOptional = babyService.findBabyById(babyId);
if (babyOptional.isEmpty()) {
return ResponseEntity.ok().body(false);
}
imageRepository.save(Image.builder()
.name("image-" + babyOptional.get().getId())
.type(files.getContentType())
.data(files.getInputStream().readAllBytes())
.baby(babyOptional.get())
.timeAdded(LocalDateTime.now())
.build());
return ResponseEntity.ok().body(true);
}
}
Я также пробовал @Type(type="org.hibernate.type.BinaryType"), но когда я import Type, он не может найти type=.
Подробнее здесь: https://stackoverflow.com/questions/785 ... ssion-is-o