我有两个表,关系由2列组成,我的第一个实体是:
public class NfCabeca implements java.io.Serializable {
@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;
@ManyToOne
@NotFound(action=NotFoundAction.IGNORE)
@MapsId("nf_cabeca_id")
@JoinColumns({
@JoinColumn(name = "sequencial" , referencedColumnName="nf_cabeca"),
@JoinColumn(name= "protocolo", referencedColumnName="protocolo")})
private NFeProtocolo protocolo;
@OneToMany(mappedBy = "nf_cabeca_id", cascade = CascadeType.ALL)
private List<NFeProtocolo> protocolos = new ArrayList<NFeProtocolo>();
public void setProtocolo(NFeProtocolo protocolo) {
this.protocolo = protocolo;
protocolos.remove(protocolo);
protocolo.setNf_cabeca_id(this);
protocolos.add(protocolo);
}
}
我的第二个实体是:
public class NFeProtocolo implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2092981840170296102L;
@Id
@ManyToOne
@JoinColumn(name = "nf_cabeca" , referencedColumnName="sequencial", updatable = false)
private NfCabeca 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;
}
问题是当我在NFeProtocolo
NFCabeca
时
NFeProtocolo nFeProtocolo = new NFeProtocolo(protocolo, operacao, dataProtocolo, status);
nFeProtocolo.setNf_cabeca_id(nf);
nf.setProtocolo(nFeProtocolo);
merge(nf);
我收到错误:
错误消息,指出列nf_cabeca无法设置为空
真的不能,但是在设置nf_cabeca
值时我做错了什么?
当观察对象nFeProtocolo时,它没有暴露nf_cabeca_id。
Protocolos内容:
[NFeProtocolo [protocolo = 333170000339533,operacao = CRIACAO,dataHora = Wed Aug 09 09:19:11 BRT 2017,status = 100]]
为什么不在外面设置nf_nf_cabeca_id?
答案 0 :(得分:0)
解决方案是创建一个
public void setProtocolos(List<NFeProtocolo> protocolos) {
this.protocolos = protocolos;
}
在NfCabeca上并将NfeProtocolo上的nf_cabeca_id更改为
@ManyToOne
@JoinColumn(name = "nf_cabeca")
private NfCabeca nf_cabeca_id;