0

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;
}
veljkost
  • 1,748
  • 21
  • 25
AgamenonD2
  • 83
  • 1
  • 11
  • 2
    Your question is too noisy, I can't really extract the info which describes your problem. Please adjust it to contain only the essential information as well as a simplistic example which can reproduce the issue. – Arnold Galovics Jul 31 '17 at 18:00

2 Answers2

0

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.

Dazak
  • 1,011
  • 2
  • 9
  • 17
  • Agree, i was already trying this solution, but i have a PK id class , so i changed NfCabeca to use NFeProtocoloId besides NFeProtocolo. testing – AgamenonD2 Jul 31 '17 at 20:23
0

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.

ujulu
  • 3,289
  • 2
  • 11
  • 14
  • just one more help, when a i map the join columns i have a conflict with my current Pk on NFCabeca, name sequencial, this column is nf_cabeca_id on NFeProtocolo tablet, so when i map as a join column o get an error about duplicating columns. – AgamenonD2 Aug 01 '17 at 19:09
  • Probably I don't understand the question, because I don't see any conflict. According to the mapping you posted, fields `nf_cabeca_id` and `protocolo` are foreign keys. If you have anything you haven't posted in your question, please edit the question so that I can see how the fields `sequencial` and `nf_cabeca_id` are related. If it helps rename the foreign key name, e.g., instead of 'nf_cabeca-id` use `nf_protocolo_id`. – ujulu Aug 01 '17 at 19:34
  • my final working code: @ManyToOne @MapsId("nf_cabeca_id") @JoinColumns({ @JoinColumn(name = "sequencial" , referencedColumnName="nf_cabeca"), @JoinColumn(name= "protocolo", referencedColumnName="protocolo")}) private NFeProtocolo protocolo; – AgamenonD2 Aug 01 '17 at 20:23