I am trying to insert items in a list in a database as single values in rows. How can I loop through the payload sent and insert all the items into individual rows? I have tried to look for examples no luck. I cannot loop and insert the values in the database.
this is the payload
{"labsigned":["234568","234567","2345678","2344556","12335677","2345677","234556","234545"]}
My controller
@RequestMapping(path = "/labreport/createrordispatched", method = RequestMethod.POST)
public ResponseEntity<?> createDispatched(@RequestBody Dispatched dispatched){
if(labDashboardService.createDispatched(dispatched)) {
return ResponseEntity.status(HttpStatus.CREATED).body(true);
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(false);
}
My Service
public boolean createDispatched(Dispatched dispatched) {
List<Dispatched> newTickets = new ArrayList<>();
dispatched.setCreatedBy(getCurrentUserEmail());
dispatched.setDateCreated(System.currentTimeMillis());
for(String labsigned:dispatched.getlabsigned()){
Dispatched ticket = new Dispatched(
dispatched.getCreatedBy(),
dispatched.getDateCreated(),
labsigned
);
newTickets.add(ticket);
}
dispatchedRepository.saveAll(newTickets);
return true;
}
My Model
@Entity
@Table(name = "DISPATCHED")
public class Dispatched {
private String id;
private String labsigned;
private Long dateCreated;
private String createdBy;
public Dispatched(){}
public Dispatched(String createdBy, Long dateCreated, String labsigned){
this.labsigned = rorlabsigned;
this.dateCreated = dateCreated;
this.createdBy = createdBy;
}
//getters and setters
the line that is giving me an error
for(String rorlabsigned:dispatched.getRorlabsigned())
How should I set my entity to accept arrays?
dispatched.getlabsigned()returnslabsignedproperty ofDispatchedwhich is String and not an iterable object. What is expected type there? It's confusing to me that you're trying to create list of new tickets from a single Dispatched object.{"labsigned":["234568","234567","2345678","2344556","12335677","2345677","234556","234545"]}