Announcement Announcement Module
Collapse
No announcement yet.
Annotated Spring MVC @ModelAttribute automapping not working with associated objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotated Spring MVC @ModelAttribute automapping not working with associated objects

    Hello,

    I am using Spring MVC with Annotations. Here's a quick outline of my problem.

    I have a Country object:

    Code:
            @Id
    	@GeneratedValue
    	@Column(updatable=false, insertable=false)
    	private Long id;
    	
    	@ManyToOne
    	private Region region;//foreign key
    
            @Column
            private String name;
    and I have a parent object Region:

    Code:
            @Id
    	@GeneratedValue
    	@Column(updatable=false, insertable=false)
    	private Long id;
    
            @Column
            private String name;
    I have a controller:

    Code:
       @RequestMapping(method = RequestMethod.POST)
        protected String onSubmit(@ModelAttribute("country")Country country) {
               System.out.println("Country Id: " + country.getId());//WORKS
               System.out.println("Region Id: " + country.getRegion().getId());//DOES NOT WORK (Region object is null)
        }
    Now on the html side of this I have a simple form that has the following hidden variables that get posted to the controller above:

    <input type="hidden" name="id" value="1">
    <input type="hidden" name="region.id" value="5">

    My problem is that the country object has the id properly automapped, but it fails to create the region object and automap that... What am I doing wrong/or missing?

    Thanks a million!

  • #2
    Is country set as the commandName on the form?

    Comment


    • #3
      I am also facing the same problem. Can anyone please provide the suggestion.

      Thanks

      Comment

      Working...
      X