В моем приложении есть 2 модели, которые имеют отношения «один ко многим» и «многие к одному».
Классы моделей:
Модель счета-фактуры
@Entity
@Data
public class Invoice implements java.io.Serializable {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;
private String business;
private String client;
private String invoiceNo;
@Enumerated(EnumType.STRING)
private InvoiceStatus status;
private String additionalInfo;
@OneToMany(mappedBy = "invoice")
private Set<InvoiceItem> items = new HashSet<>();
@CreationTimestamp
private LocalDateTime createdAt;
@UpdateTimestamp
private LocalDateTime updatedAt;
private LocalDateTime deletedAt;
}
InvoiceItem Модель
@Entity
@Data
public class InvoiceItem {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;
@ManyToOne
@JoinColumn(name="invoice_id", nullable = false)
private Invoice invoice;
private String description;
@Enumerated(EnumType.STRING)
private InvoiceItemType type;
@CreationTimestamp
private LocalDateTime createdAt;
@UpdateTimestamp
private LocalDateTime updatedAt;
private LocalDateTime deletedAt;
}
У меня есть конечная точка API, которая используется для создания invoiceItem для счета
@RestController
@RequestMapping(path="/api/v1/invoices/{invoice}/items")
public class InvoiceItemController {
@Autowired
private ModelMapper modelMapper;
@Autowired
private InvoiceItemService invoiceItemService;
@Autowired
private InvoiceService invoiceService;
@PostMapping
public ResponseEntity<InvoiceItem> addInvoiceItem(@PathVariable("invoice") String invoiceId, @RequestBody InvoiceItemCreationDto invoiceItemCreationDto) throws NotFoundException {
Optional<Invoice> invoiceOptional = invoiceService.findInvoiceById(invoiceId);
if (!invoiceOptional.isPresent()) {
throw new NotFoundException("Invoice not found");
}
InvoiceItem invoiceItem = modelMapper.map(invoiceItemCreationDto, InvoiceItem.class);
invoiceItem.setInvoice(invoiceOptional.get());
InvoiceItem savedInvoiceItem = invoiceItemService.addInvoiceItem(invoiceItem);
return new ResponseEntity<>(savedInvoiceItem, HttpStatus.CREATED);
}
}
Сервис счетов
package com.spencerfeng.invoiceservice.services;
import com.spencerfeng.invoiceservice.models.Invoice;
import com.spencerfeng.invoiceservice.repositories.InvoiceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
public class InvoiceServiceImpl implements InvoiceService {
@Autowired
InvoiceRepository invoiceRepository;
@Override
public Invoice addInvoice(Invoice invoice) {
return invoiceRepository.save(invoice);
}
@Override
public Optional<Invoice> findInvoiceById(String id) {
return invoiceRepository.findById(id);
}
}
Хранилище счетов
package com.spencerfeng.invoiceservice.repositories;
import com.spencerfeng.invoiceservice.models.Invoice;
import org.springframework.data.repository.CrudRepository;
public interface InvoiceRepository extends CrudRepository<Invoice, String> {
}
Сервис InvoiceItem
package com.spencerfeng.invoiceservice.services;
import com.spencerfeng.invoiceservice.models.InvoiceItem;
import com.spencerfeng.invoiceservice.repositories.InvoiceItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
public class InvoiceItemServiceImpl implements InvoiceItemService {
@Autowired
private InvoiceItemRepository invoiceItemRepository;
@Override
public InvoiceItem addInvoiceItem(InvoiceItem invoiceItem) {
return invoiceItemRepository.save(invoiceItem);
}
}
Но когда я вызываю эту конечную точку API для создания invoiceItem для счета-фактуры, свойство items в invoiceOptional имеет исключение «Невозможно оценить метод выражения бросил» org.hibernate.exception.GenericJDBCException, в то время как другие свойства хороши.