0

I am trying to save multiple records in a database, but am not able to save. I am able to save only 1 row at a time. I have followed this below link example. but still am getting error:

Spring mvc add multiple rows

Controller.java

This is my controller class:

   @PostMapping("/saveCREfforts")
    public String saveCREfforts(@ModelAttribute("creffortdetailsForm") CREffortDetailsListForm cREffortDetailsListForm,, Model model, BindingResult bindingResult) {


        for(CREffortDetails crEfforts : cREffortDetailsListForm.getcREffortDetails()) {
            System.out.println(" crEffortr.getCrNumber   :::: :  "+crEfforts.getCrNumber());

            cREffortsDetailsService.save(crEfforts); 
         }



        return "AddCREfforts";
    }

JSP

This is the JSP page.

I have used <form:form> tags for Spring Boot.

  <form:select name="task"  style="width:100%;" path="CREffortDetails[0].task" value="task">

    <form:option value="Documentation Creation / Updation"  >Documentation Creation / Updation</form:option>
    <form:option value="Design and development">Design and development</form:option>
    <form:option value="Build">Build</form:option>
    <form:option value="Deployment to lower environments (AM2/AM3/QA/TSTB)">Deployment to lower environments (AM2/AM3/QA/TSTB)</form:option>
    <form:option value="Unit Testing">Unit Testing</form:option>
    <form:option value="System Testing">System Testing</form:option>
    <form:option value="Unit Testing">Unit Testing</form:option>
    <form:option value="Review Efforts">Review Efforts</form:option>
    <form:option value="UAT Testing Support">UAT Testing Support</form:option>
    <form:option value="Deployment to Production">Deployment to Production</form:option>
  </form:select>          
</td>
<td>
   <form:select name="weekNumber"  style="width:100%;" path="CREffortDetails[0].weekNumber" value="weekNumber">
    <form:option value="WK39">WK39</form:option>
    <form:option value="WK40">WK40</form:option>
    <form:option value="WK41">WK41</form:option>
    <form:option value="WK42">WK42</form:option>
    <form:option value="WK43">WK43</form:option>
    <form:option value="WK44">WK44</form:option>
  </form:select>
            </td>

            <td>
            <form:input path="CREffortDetails[0].plannedHours"  value="" style="width:100%;"  />

            </td>
            <td>
            <form:input path="CREffortDetails[0].actualHours" value="" style="width:100%;" />

            </td>

         <form:input type="hidden" path="CREffortDetails[0].crNumber" style="width:100%;" />

        </tr>

         <tr>

            <td>

  <form:select name="task"  style="width:100%;" path="CREffortDetails[1].task" value="task">

    <form:option value="Documentation Creation / Updation"  >Documentation Creation / Updation</form:option>
    <form:option value="Design and development">Design and development</form:option>
    <form:option value="Build">Build</form:option>
    <form:option value="Deployment to lower environments (AM2/AM3/QA/TSTB)">Deployment to lower environments (AM2/AM3/QA/TSTB)</form:option>
    <form:option value="Unit Testing">Unit Testing</form:option>
    <form:option value="System Testing">System Testing</form:option>
    <form:option value="Unit Testing">Unit Testing</form:option>
    <form:option value="Review Efforts">Review Efforts</form:option>
    <form:option value="UAT Testing Support">UAT Testing Support</form:option>
    <form:option value="Deployment to Production">Deployment to Production</form:option>
  </form:select>          
</td>
<td>
   <form:select name="weekNumber"  style="width:100%;" path="CREffortDetails[1].weekNumber" value="weekNumber">
    <form:option value="WK39">WK39</form:option>
    <form:option value="WK40">WK40</form:option>
    <form:option value="WK41">WK41</form:option>
    <form:option value="WK42">WK42</form:option>
    <form:option value="WK43">WK43</form:option>
    <form:option value="WK44">WK44</form:option>
  </form:select>
            </td>

            <td>
            <form:input path="CREffortDetails[1].plannedHours"  value="" style="width:100%;"  />

            </td>
            <td>
            <form:input path="CREffortDetails[1].actualHours" value="" style="width:100%;" />

            </td>

         <form:input type="hidden" path="CREffortDetails[1].crNumber" style="width:100%;" />

        </tr>

        </table>



<div align="center">
<table  align="right">
<tr>
<!-- <input type="submit" value="SAVE" /> -->
 <button class="button"><span>SAVE</button>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<button class="button"><span>CLEAR</button>
</tr>
</table>
<br><br>

</form:form>

Bean class

This is the entity class. We are trying to store the form data into the database by JPA.

@Entity
@Table(name = "CREFFORTFORM")
public class CREffortDetails {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
     private String crNumber;
     private String description;
     private String task;
     private String weekNumber;
     private String plannedHours;
     private String actualHours;
     private String totalPlannedHours;
     private String totalActualHours;

        @ManyToMany(mappedBy = "creffortdetails")
        private Set<CRDataForm> crdataform;


    public Set<CRDataForm> getCrdataform() {
            return crdataform;
        }
        public void setCrdataform(Set<CRDataForm> crdataform) {
            this.crdataform = crdataform;
        }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getCrNumber() {
        return crNumber;
    }

Bean class form with list:

This is a list form for iterating multiple rows of records.

public class CREffortDetailsListForm {

    private List<CREffortDetails> cREffortDetails;


    public List<CREffortDetails> getcREffortDetails() {
        return cREffortDetails;
    }

    public void setcREffortDetails(List<CREffortDetails> cREffortDetails) {
        this.cREffortDetails = cREffortDetails;
    }



}`

I am getting this below JSP error while loading page.

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Oct 18 17:46:57 IST 2019
There was an unexpected error (type=Internal Server Error, status=500).
Invalid property 'CREffortDetails[0]' of bean class [com.demandTracker.auth.model.CREffortDetails]: Bean property 'CREffortDetails[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.NotReadablePropertyException: Invalid property 'CREffortDetails[0]' of bean class [com.demandTracker.auth.model.CREffortDetails]: Bean property 'CREffortDetails[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at 
4
  • How CREffortDetails are be setting on page?When you loading your page, are you passing an array of CREffortDetails for it?Or you load a CREffortDetailsListForm that could provide a list of CREffortDetails? Commented Oct 18, 2019 at 12:42
  • Hi, we thought CREffortDetails array values will be set on jsp page.we are not sure how it will be set values so requested for help. Do you please suggest on this.Do I need to create dummy modelattribute object to set array values? Commented Oct 18, 2019 at 14:53
  • @JulianoPacheco Can you please help on this Commented Oct 21, 2019 at 10:43
  • Could you post your controller code that load your jsp page?I guess the problem happened at this moment, because CREffortDetails is recognized but not like an Array. Maybe you need to set a list of CREffortDetails object when you load and in you jsp page change the CREffortDetails[x] to a objetct that will be add in this List. Commented Oct 21, 2019 at 16:57

1 Answer 1

0

Please provide the code from cREffortsDetailsService.

If you are using SpringDataJpa, which i think you might be because you added that tag to the question, you should be able to use the <S extends T> List<S> saveAll(Iterable<S> entities); method instead of saving one at at time using the <S extends T> S save(S entity); method.

If you want to tune the performance using hibernate batch inserting you will want to look at tuning this property:

spring:
  jpa:
    properties:
      hibernate.jdbc.batch_size: 15
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.