I have a form where I fill in a series of fields of a JPA entity, the thing is that among the fields there is a list which I am filling in through a modal, so far everything is correct, in fact I have this method made for several controllers and it goes to the perfection.
The problem is when I go to edit the form, everything loads correctly but when I click the Delete button on the list, when I reach the controller method that does it, the id of the form is different from what it should be, or it is -1 or it is the next number (which doesn't make much sense).
I leave you the part of the code that may be influencing to see if someone sees something that I could not see.
Ready
<table class="table table-striped table-condensed flip-content dataTable" th:if="${concert.choirs.size()>0}">
<thead class="flip-content">
<tr>
<th width="90%">
Nombre
</th>
<th width="10%"></th>
</tr>
</thead>
<tbody id="cuerpoTablaCorista">
<tr th:each="choir,status : ${concert.choirs}">
<td th:text="${choir.choir.name}"></td>
<td class="operations">
<a class="delete" href="/concert/delete_choir/__${status.index}__}" th:href="@{/concert/delete_choir/__${status.index}__}">
Borrar
</a>
</td>
</tr>
</tbody>
</table>
controller methods
@RequestMapping(value = "concert/delete_choir/{index}", method = RequestMethod.GET)
public String deleteChoir(@PathVariable String index, @ModelAttribute("concert") ConcertForm concertForm,
RedirectAttributes ra, Model model) {
ConcertChoir concertChoir = concertForm.getChoirs().get(Integer.parseInt(index));
concertForm.getChoirs().remove(Integer.parseInt(index));
if (null != concertChoir.getId()) {
basicServ.delete(concertChoir);
}
model.addAttribute("concert", concertForm);
return "redirect:/concert";
}
@RequestMapping(value = "/concert", method = RequestMethod.GET)
public String concert(@ModelAttribute("concert") ConcertForm concertForm, Model model) {
model.addAllAttributes(Utils.rellenaCombosConcierto(basicServ));
model.addAttribute("concert", concertForm);
return "concert/new";
}
@RequestMapping(value = "concert/edit/{idConcert}", method = RequestMethod.GET)
public String edit(@PathVariable String idConcert, Model model) {
Concert concert = (Concert) basicServ.findById(Long.valueOf(idConcert), Concert.class);
ConcertForm concertForm = new ConcertForm(concert);
model.addAttribute("concert", concertForm);
return "redirect:/concert";
}
Form
public class ConcertForm extends BaseForm {
private String name;
private String date;
private String act;
private String notes;
private Long country;
private Long province;
private String provinceName;
private Long city;
private String cityName;
private Long schedule;
private Long orchestra;
private Long director;
private Long composer;
private Long choir;
private String choirName;
private Long soloist;
private String soloistName;
private ConcertModalForm concertModal;
private List<ConcertSoloist> soloists = new ArrayList<ConcertSoloist>();
private List<ConcertChoir> choirs = new ArrayList<ConcertChoir>();
public ConcertForm(Concert c) {
this.id = c.getId();
this.createDate = c.getCreateDate();
this.createdBy = c.getCreatedBy();
this.name = c.getName();
this.date = Utils.simpleDateToString(c.getDate());
this.act = c.getAct();
this.notes = c.getNotes();
this.country = c.getCountry().getId();
this.province = c.getProvince().getId();
this.provinceName = c.getProvince().getName();
this.city = c.getCity().getId();
this.cityName = c.getCity().getName();
this.schedule = c.getSchedule().getId();
this.director = c.getDirector().getId();
this.composer = c.getComposer().getId();
this.orchestra = c.getOrchestra().getId();
this.choirs.addAll(c.getChoirs());
this.soloists.addAll(c.getSoloists());
}
public Concert getConcert(BasicService bs) {
Concert c = new Concert();
c.setId(this.id);
c.setCreateDate(this.createDate);
c.setCreatedBy(this.createdBy);
c.setName(this.name);
c.setDate(Utils.validaFecha(this.date));
c.setAct(this.act);
c.setNotes(this.notes);
c.setCountry((Country) (null != this.country && this.country != -1 ? bs.findById(this.country, Country.class)
: null));
if (null == this.id) {
c.setProvince((Province) (null != this.province && this.province != -1
? bs.findById(this.province, Province.class) : null));
c.setCity((City) (null != this.city && this.city != -1 ? bs.findById(this.city, City.class) : null));
} else {
Concert concertAux = (Concert) bs.findById(this.id, Concert.class);
if (null == this.province || this.province == -1) {
c.setProvince(concertAux.getProvince());
} else {
c.setProvince((Province) (null != this.province && this.province != -1
? bs.findById(this.province, Province.class) : null));
}
if (null == this.city || this.city == -1) {
c.setCity(concertAux.getCity());
} else {
c.setCity((City) (null != this.city && this.city != -1 ? bs.findById(this.city, City.class) : null));
}
}
c.setSchedule((Schedule) (null != this.schedule && this.schedule != -1
? bs.findById(this.schedule, Schedule.class) : null));
c.setDirector((ConcertDirector) (null != this.director && this.director != -1
? bs.findById(this.director, ConcertDirector.class) : null));
c.setComposer((ConcertComposer) (null != this.composer && this.composer != -1
? bs.findById(this.composer, ConcertComposer.class) : null));
c.setOrchestra((Orchestra) (null != this.orchestra && this.orchestra != -1
? bs.findById(this.orchestra, Orchestra.class) : null));
for (ConcertChoir cc : this.choirs) {
cc.setConcert(c);
}
for (ConcertSoloist cs : this.soloists) {
cs.setConcert(c);
}
c.getSoloists().addAll(this.soloists);
c.getChoirs().addAll(this.choirs);
return c;
}
//getters y setters
}
Ok, I have found the problem.
In the main html I had the following function embedded, which was executed every time the page was loaded
Basically it is a function that is responsible for loading the values of the city and province combos. The parameters passed to it were correct, what I don't understand is why the id changed. It was fixed anyway by replacing them with another function I was using in another controller.