• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ID isn't binded with form object

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody!

I have a form in which I select sport and country with dropdown list and there is also a text box where i write a name for the new team. When I click on save I receive this exception:



Here is my code:

Team.java


NewTeam controller




Part of NewTeam.jsp



I tried to find the cause of the error in debug mode and here is what I received.



Sport_id is 0 and drzava_id is 0 and I don't understand why. I'm out of ideas, please help.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a Spring issue not a hibernate issue so I will move this to the Spring forums.
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you bind to the select lists you are actually only going to get the id (as a string) . Spring will not know how to convert this to a Sport object. To do this you will have write your own formatter. I will give you a overly simplified example but this is just for illustration. For your purposes it would make the most sense to make your formatter take a generic entity and do a find by id. This would look into persistence context for the entity with the given id.

Once again this an oversimplified example to illustrate the point..

Controller



Person






home.jsp (only the form part)


Now the formatter this does the work converting the college id we receive into a college object. Remember this is over simplified in your case you would probably at the very least autowire in a service or repository to do a findById. If you wanted you could make it more generic to allow it to accept any entity type.




Now lastly we have to tell spring to use our new formatter in the controllers


Now when we get into the addPerson method of the controller after posting the form our Person object will have a fully populated College object on it, and there will not be any errors in the binding result which I am sure you probably had before.

 
Jozo Stan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much! I solved that issue. I couldn't post before because I still have other problems with my application.
reply
    Bookmark Topic Watch Topic
  • New Topic