如何通过JPA与FK关系正常工作

时间:2019-05-06 18:22:48

标签: java jpa foreign-keys

我在这里有这个实体关系图: https://i.imgur.com/V05i6nK.png

我正在尝试做这个项目。我已经完成了所有课程,设置了关系以及其他所有内容。

但是一旦我尝试运行并创建一个新用户,我将收到以下错误:

  

外键(FK3w0xeq6jk6vt2vi1fydpci0y9:T_PAGAMENTO [CD_CORRIDA]))   必须具有与引用的主键相同的列数   (T_CORRIDA [CD_PASSAGEIRO,CD_MOTORISTA,CD_CORRIDA])

我试图建立这些关系,但没什么不同。

@Entity
@Table(name="T_CORRIDA")
@SequenceGenerator(name="sq_corrida", sequenceName="SEQ_T_CORRIDA", allocationSize=1)
public class Corrida {

    @Id
    @GeneratedValue(generator="sq_corrida", strategy = GenerationType.SEQUENCE)
    @Column(name="CD_CORRIDA")
    private int cd_corrida;

    @Column(name="DS_ORIGEM", length = 150, nullable= false)
    private String ds_origem;

    @Column(name="DS_DESTINO", length= 150, nullable= false)
    private String ds_destino;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="DT_CORRIDA")
    private Calendar dt_corrida;

    @Column(name="VL_CORRIDA", nullable= false)
    private float vl_corrida;

    @Id
    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="CD_MOTORISTA")
    private Motorista motorista;

    @Id
    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="CD_PASSAGEIRO")
    private Passageiro passageiro;

    @OneToMany(mappedBy="corrida", cascade= CascadeType.PERSIST)
    private List<Pagamento> listaPagamento;
@Entity
@Table(name="T_MOTORISTA")
@SecondaryTable(name="T_DADOS_MOTORISTA", pkJoinColumns={@PrimaryKeyJoinColumn(name="NR_CARTEIRA")})
public class Motorista {

    @Id
    @Column(name="NR_CARTEIRA", nullable= false)
    private int nr_carteira;

    @Column(name="NM_MOTORISTA", length= 150, nullable= false)
    private String nm_motorista;

    @Temporal(TemporalType.DATE)
    @Column(name="DT_NASC")
    private Calendar dt_nasc;

    @Lob
    @Column(name="FT_CARTEIRA")
    private byte[] ft_carteira;

    @Enumerated(EnumType.STRING)
    @Column(name="DS_GENERO")
    private TipoSexo ds_genero;

    @ManyToMany(cascade=CascadeType.PERSIST)
    @JoinTable(name="T_VEICULO_MOTORISTA", 
        joinColumns=@JoinColumn(name="NR_CARTEIRA"), 
        inverseJoinColumns=@JoinColumn(name="CD_VEICULO"))
    private List<Veiculo> listaVeiculo;

    @OneToMany(mappedBy="motorista")
    private List<Corrida> corridasMotorista;

    @Column(name="NR_BANCO", table="T_DADOS_MOTORISTA")
    private int banco;

    @Column(name="NR_AGENCIA", table="T_DADOS_MOTORISTA")
    private int agencia;

    @Column(name="NR_CONTA", table="T_DADOS_MOTORISTA")
    private int conta;
@Entity
@Table(name="T_PASSAGEIRO")
@SequenceGenerator(name="seq_passageiro", sequenceName="SEQ_T_PASSAGEIRO", allocationSize=1)
public class Passageiro {

    @Id
    @Column(name="cd_passageiro")
    @GeneratedValue(generator="seq_passageiro", strategy=GenerationType.SEQUENCE)
    private int cd_passageiro;

    @Column(name="NM_PASSAGEIRO", nullable= false, length= 100)
    private String nm_passageiro;

    @Temporal(TemporalType.DATE)
    @Column(name="DT_NASCIMENTO", nullable=false)
    private Calendar dt_nascimento;

    @Enumerated(EnumType.STRING)
    @Column(name="DS_GENERO")
    private TipoSexo ds_genero;

    @OneToMany(mappedBy="passageiro")
    private List<Corrida> corridaPassageiro;
@Entity
@Table(name="T_PAGAMENTO")
@SequenceGenerator(name="seq_pgto", sequenceName="SEQ_T_PGTO", allocationSize=1)
public class Pagamento {

    @Id
    @Column(name="CD_PAGAMENTO", nullable= false)
    @GeneratedValue(generator="seq_pgto", strategy=GenerationType.SEQUENCE)
    private int cd_pagamento;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="DT_PAGAMENTO", nullable= false)
    private Calendar dt_pagamento;

    @Column(name="VL_PAGAMENTO", nullable= false)
    private float vl_pagamento;

    @Enumerated(EnumType.STRING)
    @Column(name="DS_PAGAMENTO", nullable = false)
    private TipoPagamento ds_forma_pagamento;

    @ManyToOne
    @JoinColumn(name="CD_CORRIDA")
    private Corrida corrida;

我只想知道我在做错什么,为什么我遇到外键错误,如何正确设置这些键。提前谢谢!

1 个答案:

答案 0 :(得分:1)

通过在字段cd_corrida,motorista和cd_corrida中添加@Id批注,您已指定实体Corrida具有复合PK。在这种情况下,Pagamento必须在T_PAGAMENTO表的相应3列上指定联接。

但是,由于您在Corrida的@ID字段中使用了一个自动生成的序列,因此无需复合PK,因为Corrida只能由该字段唯一标识:

@Id
@GeneratedValue(generator="sq_corrida", strategy = GenerationType.SEQUENCE)
@Column(name="CD_CORRIDA")
private int cd_corrida;

然后可以删除其他2个@Id注释。不需要ID来匹配任何PK列定义的数据库:它仅必须是唯一的,并且只需为cd_corrida指定一个序列即可,然后单独将其用作实体管理器的标识符即可。