Announcement Announcement Module
Collapse
No announcement yet.
Form object / Hibernate Sub-record / Spring Web MVC? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form object / Hibernate Sub-record / Spring Web MVC?

    I'm not really sure how best to ask my question. I'm using Spring Web MVC.

    Using Hibernate, I have a table [Object A], that has an instance of another table [Object B] in it.

    So, in my form, I can easily view and display Object A and it's corresponding Object B. The issue becomes that when I edit Object A, I need to choose Object B from a drop down list. To view Object B, I would use <c:out value="${objectA.objectB.name}"/>

    The issue is how do I get the objectB record ID from the drop down? Do I add another column to ObjectA (ObjectBIdFromWeb) that does nothing but hold the ID from the web teir so I can save a new or edited Object A? This seem cumbersome, and poorly designed. (Because Object A would have both an instance of ObjectB, but also an id field for an ObjectB, which could become inconsistent.)

    Thoughts? Hard to explain the problem, but I am hopefully someone can shed some light on this? It would not be an issue with Struts, because my form object would be different than my db object. I was trying to avoid creating another object in Spring to hold form data, but maybe that is needed?

    Thanks,
    Ryan

  • #2
    Hi there,

    It really depends how you want to bind the result from a form submission to the form object. The question is really about how you bind the ID of ObjectB in the form submission to ObjectA.

    You can create a custom binder which would look up form.objectA.objectB.id using Hibernate and bind it to the instance of form.objectA.

    On the other hand, you could create a Long (or whatever identifier object Hibernate is using for ObjectB) property in your form (eg: form.newObjectBId) and use this to retrieve the persistent instance of ObjectB. This value would be bound by Spring in the normal course of bindAndValidate() and you wouldn't need a CustomBinder object, which has drawbacks in terms of access to request, session and form objects as parameters.

    I've used both and approaches and the custom binder approach recommends itself when the relationship between ObjectA -> ObjectB is very simple. You should also consider how you're going to validate the form submission and how the choice of method would affect this.

    Mike

    However,

    Comment


    • #3
      Thanks, a custom property editor was exactly what was needed!

      Comment

      Working...
      X