Hello everybody!
I'm having problems inserting data from one form to a database.
I am using hibernate, and I can not insert a value returned from another table.
These are POJOs:
BancoBean.java
Code:
@Entity
@Table(name="tb_banco")
public class BancoBean implements Serializable{
/** Declarações **/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "bc_id", nullable=false)
private Integer bcId;
@Column(name = "bc_banco", length=50)
private String bcBanco;
@Column(name = "bc_agencia", length=4)
private Integer bcAgencia;
@Column(name = "bc_num_conta", length=5)
private Integer bcNumConta;
@OneToMany(mappedBy = "cxBanco")
private Collection<TbCaixa> tbCaixaCollection;
// getters and setters
PlanoDeContasBean.java
Code:
@Entity
@Table(name="tb_plano_de_contas")
public class PlanoDeContasBean implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pc_id")
private Integer pcId;
@Column(name = "pc_conta")
private String pcConta;
@Column(name = "pc_natureza")
private String pcNatureza;
@OneToMany(mappedBy = "cxPlanoDeContas")
private Collection<TbCaixa> tbCaixaCollection;
/** getters and setters */
CaixaBean.java
Code:
@Entity
@Table(name="tb_caixa")
public class CaixaBean implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cx_n_doc", nullable=false)
private Integer cxNDoc;
@Column(name = "cx_local")
private String cxLocal;
@Column(name = "cx_data")
@Temporal(TemporalType.DATE)
private Date cxData;
@Column(name = "cx_tipo_lancamento")
private String cxTipoLancamento;
@Column(name = "cx_historico")
private String cxHistorico;
@Column(name = "cx_valor")
private Double cxValor;
@JoinColumn(name = "cx_banco", referencedColumnName = "bc_id")
@ManyToOne
private BancoBean cxBanco;
@JoinColumn(name = "cx_plano_de_contas", referencedColumnName = "pc_id")
@ManyToOne
private PlanoDeContasBean cxPlanoDeContas;
// getters and setters
I only use one class with generics for the manipulation of the data:
Control.java
Code:
public class Control<C> implements ControlInterface{
private C c;
private final Session session;
/** Creates a new instance of Control */
public Control(C c) {
this.session = HibernateUtil.getInstance().getSession();
this.c = c;
}
public void salva() {
try{
session.save(c);
session.getTransaction().commit();
}catch (HibernateException he){
}
}
public void altera() {
try{
session.update(c);
session.getTransaction().commit();
}catch (HibernateException he){
}
}
public void exclui() {
try{
session.delete(c);
session.getTransaction().commit();
}catch (HibernateException he){
}
}
public List<?> listaTudo() {
Criteria criteria = session.createCriteria(c.getClass());
List<?> list = criteria.list();
session.beginTransaction().commit();
return list;
}
}
I've already got the forms to enter data in the entities and PlanoDeContasBean BancoBean, but I could not CaixaBean entity.
Code:
private void salvaButtonActionPerformed(java.awt.event.ActionEvent evt) {
CaixaBean caixa = new CaixaBean();
Control control = new Control(caixa);
List<PlanoDeContasBean> planos;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date data;
try {
data = (Date) formatter.parse(txtData.getText());
caixa.setCxLocal(txtLocal.getText().toUpperCase());
caixa.setCxData(data);
caixa.setCxPlanoDeContas((PlanoDeContasBean) cbxPlano.getSelectedItem());
caixa.setCxTipoLancamento(String.valueOf(cbxTipo.getSelectedItem()));
caixa.setCxHistorico(txtHistorico.getText().toUpperCase());
caixa.setCxBanco((BancoBean) cbxBanco.getSelectedItem());
caixa.setCxValor(Double.parseDouble(txtValor.getText()));
control.salva();
} catch (ParseException ex) {
ex.printStackTrace();
}
}
On lines where I seto the chart of accounts and the bank makes a mistake ...
that can not convert String to PlanoDeContasBean or BancoBean ...
Could anyone help me? Thanks in advance!