1
Hello, I am having a problem while saving data in one last table. Well, this table has two Foreign Keys and the same are Primary Keys:
When going to Controller, save the table, I use the following codes:
itv.setId();
itv.setIngresso(ing);
itv.setVenda(ven);
itv.setItvqtde(Integer.valueOf(request.getParameter("txtqtde")));
itvDao.salvarItemVenda(itv);
response.sendRedirect("my-tickents-buy.jsp");
Here is my Itemvendadao:
public void salvarItemVenda(ItemVenda itv){
try {
s = new ConnectionFactory().getSessionFactory();
tx = s.beginTransaction();
s.save(itv);
tx.commit();
s.close();
JOptionPane.showMessageDialog(null, "Item venda salva com sucesso");
} catch (Exception e) {
tx.rollback();
JOptionPane.showMessageDialog(null, "Erro ao salvar item venda" +e.getMessage());
s.close();
}
}
What do I need to put in itv.setId()? When mapped it creates this method to save the sale item ID, the sale item ID is two: the ticket object and the sale object, both are already being saved above with the code:
itv.setIngresso(ing);
itv.setVenda(ven);
Again, what we need to put on itv.setId.()?
Here is Itemvenda.java (mapped automatically by Hibernate)
public class ItemVenda implements java.io.Serializable {
private ItemVendaId id;
private Ingresso ingresso;
private Venda venda;
private int itvqtde;
public ItemVenda() {
}
public ItemVenda(ItemVendaId id, Ingresso ingresso, Venda venda, int itvqtde) {
this.id = id;
this.ingresso = ingresso;
this.venda = venda;
this.itvqtde = itvqtde;
}
public ItemVendaId getId() {
return this.id;
}
public void setId(ItemVendaId id) {
this.id = id;
}
public Ingresso getIngresso() {
return this.ingresso;
}
public void setIngresso(Ingresso ingresso) {
this.ingresso = ingresso;
}
public Venda getVenda() {
return this.venda;
}
public void setVenda(Venda venda) {
this.venda = venda;
}
public int getItvqtde() {
return this.itvqtde;
}
public void setItvqtde(int itvqtde) {
this.itvqtde = itvqtde;
}
And Itemvendaid.java is as follows:
public class ItemVendaId implements java.io.Serializable {
private int itvvencodigo;
private int itvingressocodigo;
public ItemVendaId() {
}
public ItemVendaId(int itvvencodigo, int itvingressocodigo) {
this.itvvencodigo = itvvencodigo;
this.itvingressocodigo = itvingressocodigo;
}
public int getItvvencodigo() {
return this.itvvencodigo;
}
public void setItvvencodigo(int itvvencodigo) {
this.itvvencodigo = itvvencodigo;
}
public int getItvingressocodigo() {
return this.itvingressocodigo;
}
public void setItvingressocodigo(int itvingressocodigo) {
this.itvingressocodigo = itvingressocodigo;
}
So would you recommend me to create an ID in the sales item table? It would look like this: create table itemvenda( itvcodigo integer not null auto_increment Primary key, );
– Igo
Exactly and that would be the code for manual creation. Still, I recommend that you configure Hibernate to automatically create/update tables for you (considering that it is still in the development phase and there is no important data in the database).
– Shura16