0
Spring binding many-to-many
Wow. This was a lot more complicated than I thought. After several days of research and questions I finally discovered how to make this work. Its all about custom property editors. Please note that many-to-many relationships are not the best thing to use in a production environment. They dont scale well and they are not easy to manage. I later on replaced with Solr, my new favorite implementation.
In this example, many ‘things’ have many ‘tags’ and inversely many ‘tags’ have many ‘things’.
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Set.class, “tags”, new CustomCollectionEditor(Set.class) {
protected Object convertElement(Object element) {
long id = NumberUtils.parseNumber((String) element, Long.class).longValue();
Tag tag = tagMgr.getTag(element.toString());
return new Tag(id, tag.getName);
}
});
}
Categories: Software