JPA:如何在双向OneToMany中多次映射一个实体

时间:2018-10-24 04:09:57

标签: java spring jpa mapping jpa-2.0

在正在构建的系统中,Invoice具有billedFrombilledToshippedTo,它们都是实体Branch。我不知道如何在Branch类上定义此关系的@OneToMany(如果确实是映射此关系的正确方法)这一面。这是我到目前为止所做的

分支实体

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Branch extends AuditModel implements Serializable {

    private static final long serialVersionUID = -8841725432779534218L;

    @Column(name = "branch_name", length = 25, nullable = false)
    private String branchName;

    @Column(length = 15, unique = true)
    private String gstin;

    @Column(name = "vat_tin", length = 11, nullable = true)
    private String vatTin;

    private Address address;

    private Boolean sez;

    public Boolean isSez() {
        return sez;
    }

    @OneToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "branch_contact", joinColumns = @JoinColumn(name = "branch_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "contact_id", referencedColumnName = "id"))
    private Contact contact;

}

发票实体

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Invoice extends AuditModel implements Serializable {

    private static final long serialVersionUID = 1560474818107754225L;

    @Column(unique = true, length = 20)
    private String invoiceNumber;

    @Column(nullable = false, length = 25)
    private String placeOfSupply;

    private Double roundOff;

    @Temporal(TemporalType.DATE)
    @Column(nullable = false)
    private Date invoiceDate;

    @Temporal(TemporalType.DATE)
    @Column(nullable = false)
    private Date dueDate;

    @Column(nullable = false)
    private String paymentTerms;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "invoice_line_items", joinColumns = @JoinColumn(name = "invoice_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "line_item_id", referencedColumnName = "id"))
    private List<LineItem> lineItems;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "invoice_id", referencedColumnName = "id")
    private List<Payment> payments;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "billed_from_invoices", joinColumns = @JoinColumn(name = "invoice_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "branch_id", referencedColumnName = "id"))
    private Branch billedFrom;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "customer_invoices", joinColumns = @JoinColumn(name = "invoice_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "branch_id", referencedColumnName = "id"))
    private Customer customer;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "billed_to_invoices", joinColumns = @JoinColumn(name = "invoice_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "branch_id", referencedColumnName = "id"))
    private Branch billedTo;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "shipped_to_invoices", joinColumns = @JoinColumn(name = "invoice_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "branch_id", referencedColumnName = "id"))
    private Branch shippedTo;

}

我应该能够找到

  1. 开具发票到(客户)分支机构的发票
  2. 从我(公司)分支开具的发票
  3. 发票已发货到(客户)分支

0 个答案:

没有答案