Save Many-to-Many in Spring MVC

前端 未结 1 2006
耶瑟儿~
耶瑟儿~ 2021-01-03 14:19

I got relation many to many between Restaurant and Tag. Here are my entities:

public class Restaurant {
    @Id
    @GeneratedValue
    private int id;
    (         


        
相关标签:
1条回答
  • 2021-01-03 14:50

    You will have to define a custom property editor for the tags property of your restaurant object on your controller.

    @InitBinder
        protected void initBinder(HttpServletRequest request,
                ServletRequestDataBinder binder) throws Exception {
    
            super.initBinder(request, binder);
    
            binder.registerCustomEditor(List.class, "tags",new CustomCollectionEditor(List.class){
    
                @Override
                protected Object convertElement(Object element) {
                    Tag tag = new Tag();
    
                    if (element != null) {
                        Long id = Long.valueOf(element.toString());
                        tag.setId(id);
                    }
                    return tag;
                }
            });
    
        }
    
    0 讨论(0)
提交回复
热议问题