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 7191887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:49:26+00:00 2026-05-28T19:49:26+00:00

I submit this data to add a Child entity via Ajax (POST): (See the

  • 0

I submit this data to add a Child entity via Ajax (POST):

(See the bottom of this question for entity classes definition)

name = "Child Name"
parent.id = 3

Everything is OK. The new Child entity is saved successfully.

But If don’t include the parent.id (only name is set) (submitted using POST method)

name = "Child Name"

The validation result return this JSON:

"errors":{"parent":"may not be null"}

Note the "parent" property in that JSON. It’s supposed to return parent.id not parent.

It causes problem as the field on client-side script (HTML) has the name "parent.id" not "parent".

Any suggestion How to return parent.id instead of parent ??

Here is the handler method:

@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Map<String, ?> add(@Valid Child child, BindingResult result) {

    Map<String, ?> out = new LinkedHashMap<String, ?>();

    if(result.hasErrors()){
        Map<String, String> errors = new LinkedHashMap<String, String>();
        for (FieldError error : result.getFieldErrors()) {
           errors.put(error.getField(), error.getDefaultMessage());
        }
        out.put("success", false);
        out.put("errors", errors);
        return out;
    } else {
        out.put("success", true);
    }

    return out;

}

And here are the entity classes:

class Child {
    private int id;

    @NotNull
    @Size(min = 5)
    private String name;

    @NotNull        
    private Parent parent;

    //getter and setter methods
}

class Parent {
    private int id;

    @NotNull
    private String name;

    //getter and setter methods
}

Thank you.

  • 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-28T19:49:27+00:00Added an answer on May 28, 2026 at 7:49 pm

    Thanks for @oehmiche for the suggestion.

    I end up changing the entity classes to these:

    class Child {
        @NotNull
        private int id;
    
        @NotNull
        @Size(min = 5)
        private String name;
    
        @NotNull
        @Valid
        private Parent parent = new Parent("Foo Foo");
    
        //getter and setter methods
    }
    
    class Parent {
        @NotNull
        private int id;
    
        @NotNull
        private String name;
    
        public Parent(){
        }
    
        public Parent(String name){
             setName(name);
        }
    
        //getter and setter methods
    }
    

    And this is the submitted data:

    id = 1
    name = "Child Name"
    parent.id = 3
    

    Note the id property. I end up to always set this to 1 just to bypass the @NotNull constraint. Although the id value will always be replaced by Hibernate (I use auto generation strategy).

    (I add @NotNull constraint for id of Parent as well as Child class for consistency)

    However, there is still one problem. I have to always set parent’s name just to bypass the validation constraint for name property of Parent:

    @NotNull
    @Valid
    private Parent parent = new Parent("Foo Foo");
    

    Any suggestion for this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Coffeescript: $ -> $('#new_event').submit -> $.post( $(this).attr('action') $(this).serialize() (data, textStatus,
$('#add-asset-form').submit(function(){ //if (validate_asset_form()) { $.ajax({ type: 'POST', url: 'wp-content/themes/tvmarketing/assetform/ajax-req.php', cache: false, data: $('#add-asset-form').serialize(), error:
$('form').submit(function(){ this.action=http://www.sitename.com/post; return false; }); having altered the action attribute of the form ,i
I have one image submit button like this <input id=enter name=enter type=image value=Login src=images/btn_login.jpg/>
I am trying to submit this with Ajax to a MVC3 controller: var params
Don't know what to do with this error. How to add data in SQL
On the page 'selecteditems.php' I have a form like this: <form method=POST name=selecteditems action=nextpage.php>
When i submit this form, the values just disappears from the textboxes. I like
I have this form submit code: Event.observe(window, 'load', init, false); function init() { Event.observe('addressForm',
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!

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.