Home > Software engineering >  Can not map one to one relationship in models
Can not map one to one relationship in models

Time:09-21

I should relate Order with Invoice in one-to-one relationship but I am having the following error

Unknown mappedBy in com.example.task.model.Order.invoice referenced property unknown: com.example.task.model.Invoice.Invoice

Order model:


@Entity
@Table(name = "Order")
public class Order{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = true)
    private LocalDate date;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "Order")
    @JoinColumn(name = "order_id", referencedColumnName = "id")
    private Invoice invoice;

    @OneToMany(targetEntity = Detail.class, cascade = CascadeType.ALL)
    @JoinColumn(name = "order_id", referencedColumnName = "id")
    private List<Detail> details;

and Invoice model

@Entity
@Table(name = "Invoice")
public class Invoice {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(cascade = CascadeType.ALL)
    private Order order;

    @OneToMany(targetEntity = Payment.class, cascade = CascadeType.ALL)
    @JoinColumn(name = "inv_id", referencedColumnName = "id")
    private List<Payment> payments;

    @Column(nullable = true, precision = 8, scale = 2)
    private BigDecimal amount;

    @Column(nullable = true)
    private LocalDate issued;

    @Column(nullable = true)
    private LocalDate due;

Even if I change vice versa I having same error.

CodePudding user response:

mappedBy attribute is mistyped.

Use mappedBy = "order" O lowercase

CodePudding user response:

update this lines

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "invoice_id", referencedColumnName = "id")
private Invoice invoice;

@OneToOne(mappedBy = "invoice")
private Order order;
  • Related