Hibernate中的多对一映射

时间:2017-02-07 03:18:09

标签: java hibernate jpa orm

以下映射给出错误

  

从db.KarateInvoiceDetail引用db.KarateInvoice的外键   列数错误。应该是1

发票实体:

@Id
@Column(name = "id")
private long                      id;
@OneToOne
@JoinColumn(name = "companyid")
@Id
private Company                   company;
@Id
private short                     fiscalYear;
@OneToMany(mappedBy = "karateInvoiceDetails")
private List<KarateInvoiceDetail> karateInvoiceDetails;

发票明细:

@Id
private short         seqNo;
@ManyToOne
@JoinColumns({ @JoinColumn(name = "karateInvoiceId"),
        @JoinColumn(name = "karateInvoiceCompanyId"),
        @JoinColumn(name = "karateInvoiceFiscalYear") })
private KarateInvoice invoice;
@Id
@OneToOne
@JoinColumns({ @JoinColumn(name = "studentId"),
        @JoinColumn(name = "studentCompanyId") })
private KarateStudent student;

公司实体:

@Id
private long        id;

想法是拥有一个Invoice表,其中包含(id,fiscalyear和companyId)和InvoiceDetail的复合,其复合键为(seqNo,InvoiceId,InvoiceFiscalYear和InvoiceCompanyId)。

1 个答案:

答案 0 :(得分:1)

我认为您应该在@JoinColumn注释中明确引用Invoice列:

@ManyToOne
@JoinColumns({ @JoinColumn(name = "karateInvoiceId"
                  , referencedColumnName="id"),
        @JoinColumn(name = "karateInvoiceCompanyId"
               , referencedColumnName="companyId"),
        @JoinColumn(name = "karateInvoiceFiscalYear"
               , referencedColumnName="fiscalYear") })
private KarateInvoice invoice;