JPA - 列数错误。应该是2

时间:2017-07-31 13:55:03

标签: java hibernate jpa

  

来自br.com.copagaz.inova.mobile.persistencia.entidade.viagem.nf.NfCabeca的外键引用br.com.copagaz.inova.mobile.persistencia.entidade.viagem.nf.NFeProtocolo的错误号码列。应该是2

我的问题在于一个列引用,如果我删除@ManyToOne和@JoinColumn(name =" protocolo"),系统可以正常工作,但选择不会。 我试图使用hibernate.hbm2ddl.auto自动创建FK但没有成功。 我认为nfe_operacao使用了一个合成的PK和nf_cabeca引用的ii,但是没有用。

任何人都可以提供帮助吗?

@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>();

}

这引用了这个表:

@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;
}

2 个答案:

答案 0 :(得分:0)

  

来自br.com.copagaz.inova.mobile.persistencia.entidade.viagem.nf.NfCabeca的外键引用br.com.copagaz.inova.mobile.persistencia.entidade.viagem.nf.NFeProtocolo的错误号码列。应该是2

您的问题很简单:您的实体NFeProtocolo有一个包含两列的复合Id

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;

但是您的班级NfCabeca仅通过一列来引用它:

public class NfCabeca implements java.io.Serializable {

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

解决方案:

  

复合主键通常由两个或更多基本或JDK对象类型组成。

由于你有一个复合键,你应该使用Embeddable key,有许多例子,例如thisthisthis

答案 1 :(得分:0)

我认为问题在于您的@ManyToOne映射未正确声明。由于实体NFeProtocolo具有复合主键,因此您应使用由@JoinColumns注释数组组成的@JoinColumn注释:

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

您可以选择适当的名称作为外键列名称。