1
I’m registering for Refeicao and Aliementorefeicao. To register the items of the Foodstuff you must register the Meal first. The problem is the data are being recorded, but as I want to register several meals at the same time with their respective items, the data is getting duplicated. Ex: Grape, Apple and Pear q should be different meals they are being registered within the same meal, it generates the 3 meals only that all meals contain the same items.
JSP enter the code here
<div id="geral">
Tipo de Refeicao:
<select name="idtiporefeicao">
<%
List<TipoRefeicao> trefeicao = (List<TipoRefeicao>) request.getAttribute("trefeicao");
for (TipoRefeicao tiporefeicao : trefeicao) {
%>
<option value="<%=tiporefeicao.getIdTipoRefeicao()%>"><%=tiporefeicao.getTipoRefeicao()%></option><% }%>
</select>
Dia da Refeição:
<select id="diarefeicao" name="diarefeicao">
<option value="Domingo">Domingo</option>
<option value="Segunda-Feira">Segunda-Feira</option>
<option value="Terca-Feira">Terça-Feira</option>
<option value="Quarta-Feira">Quarta-Feira</option>
<option value="Quinta-Feira">Quinta-Feira</option>
<option value="Sexta-Feira">Sexta-Feira</option>
<option value="Sabado">Sábado</option>
</select>
<a href="#" data-action="adicionar" class="adicionar-refeicao" onclick="refeicao()" >+</a>
<a href="#" data-action="excluir" >-</a>
<fieldset>
<div id="multi">
<p>
Alimento: <select name="idalimento">
<%
List<Alimento> alimentos = (List<Alimento>) request.getAttribute("alimentos");
for (Alimento alimento : alimentos) {
%>
<option value="<%=alimento.getIdAlimento()%>" data-action="select"><%=alimento.getNomeAlimento()%></option><% }%>
</select>
<label>Quantidade: </label><input type="number" id="quantidadealimentorefeicao" name="quantidadealimentorefeicao" step="0.1" min="0.1" />
Medida: <select id="tipomedidaalimentorefeicao" name="tipomedidaalimentorefeicao">
<option value="Colher Arroz">Colher(es) de Arroz </option>
<option value="Colher Café">Colher(es) de Café</option>
<option value="Colher Cha">Colher(es) de Chá</option>
<option value="Colher Sobremesa">Colher(es) de Sobremesa</option>
</select>
<a href="#" data-action="add" >+</a>
<a href="#" data-action="delete" >-</a>
</p>
</div>
</fieldset>
Servlet
String diasRefeicao[] = request.getParameterValues("diarefeicao");
String idTipoRefeicao[] = request.getParameterValues("idtiporefeicao");
Cardapio cardapio = new Cardapio();
cardapio.setIdCardapio(idCardapio);
String alimentos[] = request.getParameterValues("idalimento");
String quantidades[] = request.getParameterValues("quantidadealimentorefeicao");
String medidaalimento[] = request.getParameterValues("tipomedidaalimentorefeicao");
Integer idRefeicao = null;
try {
for (int ii = 0; ii < idTipoRefeicao.length; ii++){
TipoRefeicao tipoRefeicao = new TipoRefeicao();
tipoRefeicao.setIdTipoRefeicao(Integer.parseInt(idTipoRefeicao[ii]));
Refeicao refeicao = new Refeicao();
refeicao.setDiaRefeicao(diasRefeicao[ii].toUpperCase().trim());
refeicao.setStatusRefeicao("A");
refeicao.setSincronizaRefeicao(1);
refeicao.setTipoRefeicao(tipoRefeicao);
refeicao.setCardapio(cardapio);
RefeicaoDAOImpl daorefeicao = new RefeicaoDAOImpl();
idRefeicao = daorefeicao.cadastrar(refeicao);
if (idRefeicao != null) {
refeicao.setIdRefeicao(idRefeicao);
for (int i = 0; i < alimentos.length; i++) {
AlimentoRefeicao alimentoRefeicao = new AlimentoRefeicao();
Alimento alimento = new Alimento();
alimento.setIdAlimento(Integer.parseInt(alimentos[i]));
alimentoRefeicao.setAlimento(alimento);
alimentoRefeicao.setRefeicao(refeicao);
alimentoRefeicao.setTipoMedidaAlimentoRefeicao(medidaalimento[i]);
alimentoRefeicao.setQuantidadeAlimentoRefeicao(Double.parseDouble(quantidades[i]));
alimentoRefeicao.setSincronizaAlimentoRefeicao(1);
try{
GenericDAO dao = new AlimentoRefeicaoDAOImpl();
if (dao.cadastrar(alimentoRefeicao)) {
mensagem = "Cadastrado com sucessso ";
} else {
mensagem = "Erro ao cadastrar itens da refeição";
}
} catch (Exception ex) {
System.out.println("Erro ao cadastrar itens da refeição" + ex.getMessage());
ex.printStackTrace();
}
}
`
On your table of meals probably has a field
ID
, that isPrimary Key
. It is defined asauto_increment
? If yes, each meal added will be oneID
different, sequential in the case. Then you wouldn’t have to worry about having to generate aID
orSerial
different for each meal, due to the fact that each meal will have aID
automatically generated and never repeated.– Diego Souza
It’s auto_increment, but it’s not generating that ID. Any idea what it might be ?
– Leticia Noni
The name of your column is
IdAlimento
? It’s not incrementing because you put it in the code.alimento.setIdAlimento(Integer.parseInt(alimentos[i]));
. You have to take it out so he can automatically enter it through the database.– Diego Souza
It’s the original thing. Because I want to register several meals at the same time and within this meal I would register quantity, food and as it is in another class.
– Leticia Noni
But the program arrives to register the product ? Or it gives some error ?
– Diego Souza
I made some changes to the project and now the data duplication is happening. I changed the publication so that you understand better.
– Leticia Noni
Leticia you completely changed the question, this is not recommended. The tip is, do not change a question completely, the ideal is to post the solution to the question and if you have generated another doubt, create a new one. In this case, as there is no answer, I believe that it will not create major problems.
– user28595
OK thanks, it was bad
– Leticia Noni