0
I don’t know if it’s right the way I’m doing it, but I got a problem:
java.lang.Illegalargumentexception: id to load is required for loading.
When I try to remove a template from the list, and also the list of selected templates, listaCompra
on display in xhtml
, does not display template information.
@ManagedBean(name = "RegistrarCompra")
@SessionScoped
public class RegistrarCompraRegra {
private Compra compra;
private Template template;
private Compra_DAO cDao;
private Collection<Template> homeTemplate;
private int numeroTemplates;
public RegistrarCompraRegra() {
compra = new Compra();
}
public String novo() {
compra = new Compra();
return null;
}
public String gravar() {
FacesContext fc = FacesContext.getCurrentInstance();
FacesMessage fm;
cDao = new Compra_DAO();
if (this.compra.getCodCompra() == null) {
try {
cDao.gravar(compra);
fm = new FacesMessage("Sucesso!");
fc.addMessage(null, fm);
} catch (Exception ex) {
fm = new FacesMessage(ex.getMessage());
fc.addMessage(null, fm);
}
} else {
cDao.alterar(compra);
fm = new FacesMessage("Alteração realizada com sucesso!");
fc.addMessage(null, fm);
}
return "Carrinho";
}
public String adicionaCarrinho() {
this.numeroTemplates +=1;
this.template = new Template();
this.compra.getListaCompra().add(template);
return null;
}
public String removeCarrinho() {
this.numeroTemplates -=1;
Compra_DAO cDAO = new Compra_DAO();
compra.getListaCompra().remove(template);
this.compra = cDAO.atualizar(this.compra);
return null;
}
public Collection<Template> getHomeTemplate() {
Template_DAO tDAO = new Template_DAO();
this.homeTemplate = tDAO.todos();
return this.homeTemplate;
}
//Getters e Setters & Hash
@Entity
@Table(name = "Compra")
@SequenceGenerator(initialValue = 1, name = "Gerador_codCompra", sequenceName = "Gerador_codCompra", allocationSize = 1)
public class Compra implements Serializable {
private static final long serialVersionUID = -4846590792485118024L;
@Id
@GeneratedValue(generator = "Gerador_codCompra", strategy = GenerationType.SEQUENCE)
private Long codCompra;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "codTemplate")
private Collection<Template> listaCompra;
@ManyToOne
@JoinColumn(name = "codCliente")
private Cliente cliente;
public Compra() {
this.listaCompra = new ArrayList<>();
}
// XHTML
<h:dataTable value="#{RegistrarCompra.compra.listaCompra}" var="Carrinhotemplate" rules="rows" class="tabela" border="1">
<h:inputHidden value="#{Carrinhotemplate.codTemplate}" />
<h:column id="Nome" class="cabecalho">
<f:facet name="header">
<h:outputText value="Nome"></h:outputText>
</f:facet>
<h:outputText value="#{Carrinhotemplate.nome}"></h:outputText>
</h:column>
<h:column id="Categoria" class="cabecalho">
<f:facet name="header">
<h:outputText value="Categoria"></h:outputText>
</f:facet>
<h:outputText value="#{Carrinhotemplate.categoria}" />
</h:column>
<h:column id="Valor" class="cabecalho">
<f:facet name="header">
<h:outputText value="Valor"></h:outputText>
</f:facet>
<h:outputText value="#{Carrinhotemplate.valor}" />
</h:column>
<h:column id="column4" class="ultimo">
<h:commandButton action="#{RegistrarCompra.removeCarrinho()}" value="Remover do Carrinho">
<f:setPropertyActionListener target="#{RegistrarCompra.template}" value="#{template}"/>
</h:commandButton>
</h:column>
When removing from the cart, you are setting the value "#{template}" in "#{Registrarcompra.template}", but I did not find value anywhere else in XHTML. That’s not why he can’t remove?
– Marcus Martins
Regarding not displaying information, the session may not be open. As the listCompra is a Join with Template, it cannot load the data.
– Marcus Martins