简体   繁体   English

主外键的JPA-Hibernate映射

[英]JPA-Hibernate mapping of primary foreign key

I have a little problem with JPA mapping. 我对JPA映射有一点问题。 I want do this: 我要这样做:

I have a table 'sale', and it has a id ( ven_cod ). 我有一张桌子'sale',它有一个ID(ven_cod)。

And i have a table 'credit_sale', its pk shall be a sales pk that means some sales can be credit_sale. 我有一个表'credit_sale',它的pk应该是一个销售pk,这意味着一些销售可以是credit_sale。

For instance, i have 2 sales, with code 01 and 02. The second one is a credit_sale, than in the table 'credit_sales' i'll have a register with pk 02. 例如,我有2个销售,代码分别为01和02。第二个销售是credit_sale,比表“ credit_sales”中的ID为pk 02。

How can i map this with jpa-hibernate ?? 我如何用jpa-hibernate映射它? i tried this, but didn't work: 我试过了,但是没有用:

@Entity
@Table(name = "venda_credito")
public class VendaCredito {

    private long cod;
    private Cliente cliente;
    private StatusPagamento statusPagamento;
    private Date dataPagamento;

    @Id
    @JoinColumn(name = "ven_cod")
    @OneToOne
    public long getCod() {
        return cod;
    }

    .
    .
    .
@Entity
@Table(name = "venda")
public class Venda  {

    private long cod;


    @Id
    @GeneratedValue
    @Column(name = "ven_cod")
    public long getCod() {
        return cod;
    }

    .
    .
    .

What must i do to this work ? 我必须做什么工作?

You can use a derived identity. 您可以使用派生身份。

Change VendaCredito to look like this: VendaCredito更改为以下形式:

@Entity
@Table(name = "venda_credito")
public class VendaCredito {

    private long cod;
    private Venda venda;
    private Cliente cliente;
    private StatusPagamento statusPagamento;
    private Date dataPagamento;

    @Id
    public long getCod() {
        return cod;
    }

    @MapsId
    @JoinColumn(name = "ven_cod")
    @OneToOne
    public long getCod() {
        return cod;
    }
    .
    .
    .

This is discussed in the JPA 2.1 spec section 2.4.1.3 ex. 在JPA 2.1规范的第2.4.1.3节中对此进行了讨论。 4. 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM