I am using Hibernate’s @OrderColumn annotation to order a list in my database. It correctly generates the column in the database table to store index and also stores the correct index initially.
But when I try to change the ordering in the list and then use Spring Roo’s merge() method to update the database, the ordering doesn’t change.
Here is the code for my entity:
@RooJavaBean
@RooToString
@RooEntity
public class Story {
@ManyToMany(cascade = CascadeType.ALL)
@OrderColumn
private List<Slide> slides = new ArrayList<Slide>();
@ManyToOne
private Question question;
private String title;
}
Here is the update method which calls merge function:
@RequestMapping(method = RequestMethod.PUT)
public String StoryController.update(@Valid Story story, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
if (bindingResult.hasErrors()) {
uiModel.addAttribute("story", story);
return "storys/update";
}
uiModel.asMap().clear();
story.merge();
return "redirect:/storys/" + encodeUrlPathSegment(story.getId().toString(), httpServletRequest);
}
I have checked using debugging that the story object has the updated indices in the slides list.
When you say:
Are you sure about that? When I created a sample project with hibernate mapping similar to your Stories and Slides nothing was being inserted into the intermediary table. Make sure your join table between stories and slides is being populated at all. You can add
to
log4j.propertiesand inspect the log.I had to add an explicit
@JoinTableto the association so that all operations were cascaded correctly.