主外键的JPA-Hibernate映射
JPA-Hibernate mapping of primary foreign key
我在 JPA 映射方面遇到了一点问题。
我想这样做:
我有一个 table 'sale',它有一个 ID ( ven_cod )。
我有一个 table 'credit_sale',它的 pk 应该是一个销售 pk,这意味着一些销售可以是 credit_sale。
例如,我有 2 个销售,代码为 01 和 02。第二个是 credit_sale,而不是 table 'credit_sales' 我将有一个注册02.
我如何用 jpa-hibernate 映射它??我试过了,但没用:
@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;
}
.
.
.
我必须对这项工作做什么?
您可以使用派生身份。
将 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;
}
.
.
.
这在 JPA 2.1 规范第 2.4.1.3 节 ex 中进行了讨论。 4.
我在 JPA 映射方面遇到了一点问题。 我想这样做:
我有一个 table 'sale',它有一个 ID ( ven_cod )。
我有一个 table 'credit_sale',它的 pk 应该是一个销售 pk,这意味着一些销售可以是 credit_sale。
例如,我有 2 个销售,代码为 01 和 02。第二个是 credit_sale,而不是 table 'credit_sales' 我将有一个注册02.
我如何用 jpa-hibernate 映射它??我试过了,但没用:
@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;
}
.
.
.
我必须对这项工作做什么?
您可以使用派生身份。
将 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;
}
.
.
.
这在 JPA 2.1 规范第 2.4.1.3 节 ex 中进行了讨论。 4.