tickets
complaint_id (PK, VARCHAR(255))
attachment
complaint_id (FK, VARCHAR(255))
другие поля: id, file_name, file_type, data
Схема выглядит правильно: оба столбца — VARCHAR(255), и внешний ключ fk_attachment_complaint существует.
Код: Выделить всё
SELECT a.id, a.file_name, a.complaint_id, t.complaint_id
FROM attachment a
JOIN tickets t ON a.complaint_id = t.complaint_id
WHERE t.complaint_id = 'IND-2025-0001';
Объекты:
Код: Выделить всё
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "TICKETS")
public class Tickets {
@Id
@GeneratedValue(generator = "complaint-id-generator")
@GenericGenerator(name = "complaint-id-generator", strategy = "com.example.ticket.utils.ComplaintIdGenerator")
@Column(name = "complaint_id")
private String complaintId;
// other fields...
}
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "ATTACHMENT")
public class Attachment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String fileName;
private String fileType;
private byte[] data;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "complaint_id", referencedColumnName = "complaint_id", nullable = false)
private Tickets ticket;
}
Код: Выделить всё
@Query("SELECT a FROM Attachment a WHERE a.ticket.complaintId = :complaintId")
List findByComplaintId(@Param("complaintId") String complaintId);
Код: Выделить всё
@PostMapping("/attachments/{complaintId}")
public ResponseEntity handleAttachment(
@PathVariable String complaintId,
@RequestParam(value = "file", required = false) MultipartFile file) {
if (file == null || file.isEmpty()) {
// No file uploaded — just return all attachments
List attachments = attachmentService.getAttachmentsByComplaintId(complaintId);
return ResponseEntity.ok(attachments);
}
// File uploaded — save it, then return updated list
attachmentService.uploadImage(complaintId, file);
List updatedAttachments = attachmentService.getAttachmentsByComplaintId(complaintId);
return ResponseEntity.ok(updatedAttachments);
}
@GetMapping("/attachments/{complaintId}")
public ResponseEntity getAttachments(@PathVariable String complaintId) {
List attachments = attachmentService.getAttachmentsByComplaintId(complaintId);
return ResponseEntity.ok(attachments);
}
Когда я вызываю этот метод репозитория из конечной точки GET, Hibernate генерирует правильный SQL:
Код: Выделить всё
select a.id, a.file_name, a.file_type, a.complaint_id
from attachment a
left outer join tickets t on a.complaint_id = t.complaint_id
where t.complaint_id = ?
Как ни странно, когда тот же метод репозитория вызывается в конечной точке POST (после загрузки файла), он выполняется успешно и возвращает полный список вложений.
Вопрос:
Почему метод репозитория возвращает пустой список при GET, но правильно работает при POST в Spring Boot 2.7.x?
Какой аспект обработки транзакций, управления сеансами, сопоставления сущностей или поведения JPA может объяснить разницу между конечными точками?
Подробнее здесь: https://stackoverflow.com/questions/798 ... orks-on-po
Мобильная версия