简体   繁体   中英

JPA - wrong number of column. should be 2

A Foreign key refering br.com.copagaz.inova.mobile.persistencia.entidade.viagem.nf.NFeProtocolo from br.com.copagaz.inova.mobile.persistencia.entidade.viagem.nf.NfCabeca has the wrong number of column. should be 2

My problem is in one column reference, if i remove @ManyToOne and @JoinColumn(name = "protocolo"), the system works but the selects does not. i tried to use hibernate.hbm2ddl.auto to auto create the FKs but with no success. I think the nfe_operacao use a composed PK, and nf_cabeca reference's ii, but did not work.

Any one could help?

@Entity
@Table(name = "nf_cabeca", schema = "mobile", uniqueConstraints = 
    {@UniqueConstraint(columnNames = 
        {"NUMERO_FILIAL","serie_nota","numero_nota"})})
public class NfCabeca implements java.io.Serializable {

    private static final long serialVersionUID = -921687831233770627L;

    @Id
    @SequenceGenerator(name = "nf_cabeca_sequencial_seq", sequenceName = "nf_cabeca_sequencial_seq", schema = "mobile", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "nf_cabeca_sequencial_seq")
    @Column(name = "sequencial", insertable = false, updatable = false)
    private long sequencial;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "data_hora", nullable = false, length = 29)
    private Date dataHora;

    @Column(name = "valor_total", nullable = false, precision = 17, scale = 17)
    private Double valorTotal;

    @Column(name = "cancelada")
    private Integer cancelada;

    @Temporal(TemporalType.DATE)
    @Column(name = "data_vencimento", length = 13)
    private Date dataVencimento;

    @Column(name = "boleto", length = 17)
    private String boleto;

    @ManyToOne
    @JoinColumn(name = "protocolo")
    private NFeProtocolo protocolo;

    @Column(name = "chave")
    private String chave;

    @Column(name = "status_nf")
    private Integer statusNf;

    @Column(name = "status_danfe")
    private Integer statusDanfe;

    @Column(name = "modelo", length = 3)
    private String modelo;

    @Column(name = "reconciliada")
    private boolean reconciliada = false;

    @OneToMany(mappedBy = "nfCabeca", cascade = CascadeType.MERGE)
    private List<NfObservacao> nfObservacao;

    @OneToMany(mappedBy = "nfCabeca", cascade = CascadeType.ALL)
    private List<NfItens> nfItens;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "nf_cabeca")
    private List<NFeProtocolo> protocolos = new ArrayList<NFeProtocolo>();

}

This references this table:

@Entity
@IdClass(NFeProtocoloId.class)
@Table(name = "nfe_protocolo", schema = "mobile")
public class NFeProtocolo implements Serializable {

    private static final long serialVersionUID = 2092981840170296102L;

    @Id
    @Column(name = "nf_cabeca", length = 100, insertable = false, updatable = false)
    private long nf_cabeca_id;

    @Id
    @Column(name = "protocolo", length = 100)
    private String protocolo;

    @Column(name = "operacao", length = 15, nullable = false)
    @Enumerated(EnumType.STRING)
    private NFeProtocoloOperacao operacao;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "data_hora", length = 29, nullable = false)
    private Date dataHora;

    @Column(name = "status", length = 10)
    private String status;
}

A Foreign key refering br.com.copagaz.inova.mobile.persistencia.entidade.viagem.nf.NFeProtocolo from br.com.copagaz.inova.mobile.persistencia.entidade.viagem.nf.NfCabeca has the wrong number of column. should be 2

Your problem is simple: Your Entity NFeProtocolo has a composite Id with two columns:

public class NFeProtocolo implements Serializable {

@Id
@Column(name = "nf_cabeca", length = 100, insertable = false, updatable = false)
private long nf_cabeca_id;

@Id
@Column(name = "protocolo", length = 100)
private String protocolo;

But your class NfCabeca is referencing it through only one column:

public class NfCabeca implements java.io.Serializable {

@ManyToOne
@JoinColumn(name = "protocolo")
private NFeProtocolo protocolo;

The solution:

A composite primary key is usually made up of two or more primitive or JDK object types.

As you have a composite key, you should use an Embeddable key , there are many examples about it like this , this and this .

I think the problem is that your @ManyToOne mapping is not correctly declared. As the the entity NFeProtocolo has a composite primary key, you should use @JoinColumns annotation that consists of an array of @JoinColumn annotations:

@ManyToOne
@JoinColumns({@JoinColumn(name = "nf_cabeca_id", referncedColumnName="nf_cabeca_id"), 
              @JoinColumn(name= "protocolo", referencedColumnName="protocolo")})
private NFeProtocolo protocolo;

You can choose an appropriate name as a foreign key column name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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