0

I have problems with Many to One relationship because I don't show correctly the entity.

Could anyone helps to me ?

I attached my code.

Invoice

@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;

Controller

@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);
    }

}

The response when I call a endpoint 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
]

My question :Why is not showing correctly the response the ManyToOne entity if I have all correct ?

pepe
  • 335
  • 2
  • 11
  • I don't follow the problem. What you were expecting? Is the property "product" not null for the example you are displaying? – fwerther Mar 21 '20 at 15:46
  • If the problem is because you're exposing the contents of the invoice (and thus the lines over again), take a look at the @JsonBackReference – fwerther Mar 21 '20 at 15:56
  • I don't know if you could see the number 2 and 3 , I don't know why see the information about number 2 and 3 in invoiceList ... – pepe Mar 21 '20 at 18:47
  • It's how [JsonIdentityInfo](https://www.logicbig.com/tutorials/misc/jackson/json-identity-info-annotation.html) works, as [fwerther](https://stackoverflow.com/users/8025654/fwerther) said. – Stefan Golubović Mar 21 '20 at 22:01

1 Answers1

0

If I understood your problem correctly after the comments, you are bothered by the "numbers" that are displayed. Those numbers are used to avoid infinite recursion, and they refer to entities that were already displayed.

So the number "2" would be this actually:

{
  "id": 2,
  "product": "Tag1-FR",
  "invoice": 1
}

If a representation like that is not used, then the whole invoice and it's items would be repeated infinitely.

There are several different ways to avoid this behavior, such as using @JsonIgnore or @JsonBackReference and @JsonManagedReference. Take a look at this explanation about their differences

Difference between @JsonIgnore and @JsonBackReference, @JsonManagedReference

fwerther
  • 98
  • 6