0

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?

2
  • 1
    dispatched.getlabsigned() returns labsigned property of Dispatched which 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. Commented Jun 13, 2021 at 9:17
  • It expects an Iterable object as the payload comes as {"labsigned":["234568","234567","2345678","2344556","12335677","2345677","234556","234545"]} Commented Jun 13, 2021 at 9:21

2 Answers 2

2

Declare your labsigned field as Set or use String.split like:

for(String rorlabsigned : dispatched.getRorlabsigned().split(",")) { ... }
Sign up to request clarification or add additional context in comments.

Comments

0

The error message indicates that your getter dispatched.getRorlabsigned() does not return a Collection. It returns a single String instead. You can't loop over a String using foreach. I guess you need a Set<String> there. Try to refactor that part of your code.

6 Comments

Hi @Marcus, I need to Set<String> where? Thank you for your response
private Set<String> labsigned; in the model
I end up getting Basic' attribute type should not be a container
try using @ElementCollection annotation above. You can check out thread here - stackoverflow.com/questions/21059451/…
Gives me another error Could not determine type for: java.util.Set, at table: BDISPATCHED, for columns: [org.hibernate.mapping.Column(labsigned)]
|

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.