У меня проблемы с отношением «многие к одному», потому что я неправильно показываю сущность.
Может ли кто-нибудь мне помочь?
Я прикрепил свой код.
Счет
@Entity
@Table(name = "invoices")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
public class Invoice {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String clave;
@OneToMany(mappedBy = "invoice", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, orphanRemoval = true)
private List<InvoiceLine> lines;
InvoiceLines
@Entity
@Table(name = "invoice_lines")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
public class InvoiceLine {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "product", nullable = false)
private String product;
@ManyToOne
@JoinColumn(name = "invoice_id", referencedColumnName = "id", nullable = false)
private Invoice invoice;
Контроллер
@RestController
public class InvoiceController{
@Autowired
private InvoiceRepository invoiceRepository;
@Autowired
private InvoiceLineRepository invoiceLineRepository;
@GetMapping("/")
public Iterable<Invoice> findAllnvoices(){
return invoiceRepository.findAll();
}
@GetMapping("/invoiceLine")
public Iterable<InvoiceLine> findAllInvoiceLine(){
return invoiceLineRepository.findAll();
}
@GetMapping("/{id}")
public Optional<Invoice> findTagByInvoice(@PathVariable("id") Long id){
return invoiceRepository.findById(id);
}
}
Ответ когда я вызываю конечную точку invoiceLine:
[
{
"id": 1,
"product": "Tag1-ES",
"invoice": {
"id": 1,
"clave": "Tag1",
"lines": [
1,
{
"id": 2,
"product": "Tag1-FR",
"invoice": 1
},
{
"id": 3,
"product": "Tag1-IT",
"invoice": 1
}
]
}
},
2,
3
]
Мой вопрос: почему неправильно отображается ответ сущности ManyToOne, если у меня все верно?