Search code examples
javahibernatejpahibernate-mapping

How to save child object automatically using a parent object using Hibernate and JPA?


I have onetoone association. The child class has a foreign key which is a primary key in the parent class. My problem is I am unable to save the object of child class with the parent class.

My add method is as follows:

@Transactional
public void add(ParentDTO parentDTO) {
    parentDTO.setChild(child);
    child.setParent(parentDTO);
    Parent parent = mapper.map(parentDTO, Parent.class);
    parentRepository.save(parent);
}

JSP code is as follows;

<form:form action="user.do" method="POST" commandName="user">
<table>
    <tr>
        <td>User ID</td>
        <td><form:input path="userId" /></td>
    </tr>
    <tr>
        <td>Username</td>
        <td><form:input path="username" /></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><form:password path="password" /></td>
    </tr>
    <tr>
        <td>First Name</td>
        <td><form:input path="firstName" /></td>
    </tr>
    <tr>
        <td>Last Name</td>
        <td><form:input path="lastName" /></td>
    </tr>
    <tr>
        <td>Active</td>
        <td><form:checkbox path="active" /></td>
    </tr>
    <tr>
        <td>ID</td>
        <td><form:label path="ethnicity.ethnicityId" /></td>
    </tr>
    <tr>
        <td>Nationality</td>
        <td><form:input path="ethnicity.nationality" /></td>
    </tr>
    <tr>
        <td>Race</td>
        <td><form:input path="ethnicity.race" /></td>
    </tr>
    <tr>
        <td>Region</td>
        <td><form:input path="ethnicity.region" /></td>
    </tr>
    <tr>
        <td>Religion</td>
        <td><form:input path="ethnicity.religion" /></td>
    </tr>
            <tr>
        <td colspan="2">
            <input type="submit" name="action" value="Add" />
            <input type="submit" name="action" value="Edit" />
            <input type="submit" name="action" value="Delete" />
            <input type="submit" name="action" value="Search" />
        </td>
    </tr>
</table>

Controller code

@RequestMapping(value="/user.do", method=RequestMethod.POST)
public String doActions(@ModelAttribute UserDTO user,
        BindingResult result, @RequestParam String action,
        Map<String, Object> map){
    UserDTO userResult = new UserDTO();
    switch(action.toLowerCase()){
    case "add":
        userService.add(user);
        break;

Solution

  • You are using spring form so in controller you will get your ethnicity object in model attribute.

    you can go like as below :

    childDTO.setNationality(user.getEthnicity().getNationality());
    

    If you are using servlet you can get value with

    childDTO.setNationality(request.getParameter("nationality"));
    

    And in Spring controller there are other ways as well to get your jsp input:

       @Transactional
        public void add(ParentDTO parentDTO) {
    //your child object should have all the property set here so that all values will be automatically inserted.
    parentDTO.setChild(child);
    child.setParent(parentDTO);
    Parent parent = mapper.map(parentDTO, Parent.class);
    parentRepository.save(parent);
    //Even you can set property here
    

    }