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


i have onetoone association. child class has foreign key primary key in parent class. problem unable save object of child class parent class.

my add method 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 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; 

you using spring form in controller ethnicity object in model attribute.

you can go below :

childdto.setnationality(user.getethnicity().getnationality()); 

if using servlet can value with

childdto.setnationality(request.getparameter("nationality")); 

and in spring controller there other ways jsp input:

   @transactional     public void add(parentdto parentdto) { //your child object should have property set here values automatically inserted. parentdto.setchild(child); child.setparent(parentdto); parent parent = mapper.map(parentdto, parent.class); parentrepository.save(parent); //even can set property here 

}


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -