Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 282873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:19:32+00:00 2026-05-12T05:19:32+00:00

I have a domain class named Parent as follows public class Parent { public

  • 0

I have a domain class named Parent as follows

public class Parent {

    public List<Child> childList = new ArrayList<Child>();

    public void setChildList(List<Child> childList) {
        this.childList = childList;
    }

    public List<Child> getChildList() {
        return childList;
    }

    public void addChild(Child child) {
        childList.add(child);

        child.setParent(this);
    }

    // other getters and setters

}

Notice Parent/Child is a bidirectional relationship. A addChild convenience method takes care of the bidirectional references.

And a Child class as follows

public class Child {

    private Parent parent;

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;   
    }

    private String descripton;

    // other getters and setters

}

I have a Spring form as follows

<form:form method="POST" action="addCommand.xhtml" commandName="command">
    <div>
        <p>1° child:</p>
        <form:input path="childList[0].description"/>    
    </div>
    <div>
        <p>2° child:</p>
        <form:input path="childList[1].description"/>
    </div>
    <div>
        <p>3° child:</p>    
        <form:input path="childList[2].description"/>
    </div>
    <input type="submit"/>
</form:form>

Has someone an idea how to bind a child element through addChild convenience method ? I need it because i want to save a cascace Parent/Child bidirectional relationship in Hibernate.

Added to Jacob’s answer:

Hi Jacob,

The purpose of AutoPopulatingList.ElementFactory’s createElement(int index) method is sets up each Child created “just in time”. Then if you use something as follows:

protected Object formBackingObject(HttpServletRequest request) throws Exception {
    Parent parent = new Parent();
    parent.setChildList(
        new AutoPopulatingList.ElementFactory() {
            public Object createElement(int index) {
                Child child = new Child();

                // You have just added A NEW CHILD to command object
                parent.addChild(child);

                return child;
            }
        });

    return parent;
}

So if i submit THREE Child objects, my Parent command object will have SIX Child objects instead.

You could TEST it according to:

public class BinderTest {

   private Parent parent;

   private MockHttpServletRequest request;
   private ServletRequestDataBinder binder;

   @BeforeMethod
   public void setUp() {
       parent = new Parent();
       parent.setChildList(
        new AutoPopulatingList.ElementFactory() {
            public Object createElement(int index) {
                Child child = new Child();

                // You have just added A NEW CHILD to command object
                parent.addChild(child);

                return child;
            }
        });

       request = new MockHttpServletRequest();
       binder = new ServletRequestDataBinder(parent, "parent")
   }

   @DataProvider(name="bindParameter")
   public Object [][] bindParameter() {
       return new Object [][] {
           {new String [] {"d0", "d1", "d2", "d3", "d4"}}
       };
   }

   @Test(dataProvider="bindParameter")
   public void bind(String [] descriptionArray) {

       // Notice five childs created (descriptionArray.length)
       int i = 0;
       for(String description: descriptionArray)
           request.addParameter("childList[" + i++ + "].description", description);

       // It will fail
       // parent.getChildList().size() return TEN Child objects
       assertEquals(parent.getChildList().size(), descriptionArray.length);
   }

}

So in order to pass it, you need the following

   parent = new Parent();
   parent.setChildList(
    new AutoPopulatingList.ElementFactory() {
        public Object createElement(int index) {
            Child child = new Child();

            // child now reference parent
            child.setParent(parent);

            return child;
        }
    });

When the form is submitted, parent comand object will reference Child objects because they has been added to childList. Both bidirectional references have been set up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:19:33+00:00Added an answer on May 12, 2026 at 5:19 am

    When you bind to childList[1].description, this gets translated behind the scenes into getChildList().get(1).setDescription().

    Question: does this run as written? I’d expect it to fail, since the list doesn’t have any objects in it to bind to, unless somewhere else (say in a controller) you are adding some empty Child objects to the childList (the alternative to this is to use some kind of lazy list such as Spring’s AutoPopulatingList).

    If you are either manually adding Child objects, or using a lazy list (which allows you to specify a factory method for creating new objects), I’d set up the parent/child relationship at the point the Child objects are added to the list.

    To set up the parent/child relationship if using a lazy list: For example, if you’re using Spring’s AutoPopulatingList, you can pass in a custom class that implements an ElementFactory interface, which has one method createElement. You could create a ChildElementFactory that takes a Parent object in its constructor, and uses it to set up the relationship.

    Maybe code will be clearer:

    public class ChildElementFactory implements AutoPopulatingList.ElementFactory {
      private Parent parent;
    
      public ChildElementFactory(Parent parent) {
         this.parent = parent;
      }
    
      public Object createElement(int index) {
        Child child = new Child();
        parent.addChild(child);
        return child;
      }
    }
    
    public class Parent {
        public List<Child> childList = new AutoPopulatingList(new ChildElementFactory(this));
    
    // and the rest of Parent is the same, as is child
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain class named DaySchedule like this: class DaySchedule { Date Todaysdate
I have the following domain: public class FileInformation { public String FileName; public String
If I have a List in a Grails domain class, is there a way
I'm having a hard time figuring this validation problem. I have one parent domain
Assume we have a domain class public class Incident { [Key] public virtual int
I am new bie in grails and i have following query. Problem Domain: Parent
Lets say I have a domain class named Tag in my Grails application. class
If I have the following domain object: public class Customer { public virtual Guid
I am creating a domain class named HOLIDAY in grails.I need to have a
I have a domain class: class Author { String name static hasMany = [superFantasticAndAwesomeBooks:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.