0

I'm trying to save a MeasurementsFile object to a repository and the repository.save(measurementsFile) does nothing. I've tried googling it but I find nothing similar.
Weirdly the code did work before when my parser which created the List<MeasurementsRow> didn't work and returned an empty list. I tried saving it without setting the ID so this question isn't relevant.

Upload method in my controller which calls the fileservice saveMeasurementsFile method: (I hardcoded the id to 0L while testing. When running this just returns the "could not upload..." message.):

 @PostMapping("/upload")
    public ResponseEntity<ResponseMessage> uploadMeasurements(@RequestParam("file") MultipartFile file) {
        //what about the other parameters such as id, has2ndMic, hasBackboard and references speeds?
        String message = "";
        logger.info("POST: excel");
        if (ExcelHelper.hasExcelFormat(file)) {
            try {
                logger.info("file name: "+file.getOriginalFilename());
                fileService.saveMeasurementsFile(file,0L);
                message = "Uploaded the file successfully: " + file.getOriginalFilename();
                return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
            } catch (Exception e) {
                message = "Could not upload the file: " + file.getOriginalFilename() + "!";
                return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
            }
        }
        message = "Please upload an excel file!";
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseMessage(message));
    }

The saveMeasurementsFile() method in the fileservice (the program stops after the "inserted MeasurementRows" message. If i leave out the .setID and .setMeasurementsMic1 the problem still occurs):

public void saveMeasurementsFile(MultipartFile file, long id) {
        try {
            logger.info("reached before excelhelper is called, input stream:"+file.getInputStream()+" with name "+ file.getOriginalFilename() +" and contenttype: "+ file.getContentType());

            List<MeasurementRow> measurementRows = ExcelHelper.toMeasurementRows(file.getInputStream());
            logger.info(""+measurementRows.size()+" rows");
            MeasurementsFile measurementsFile = new MeasurementsFile();
            logger.info("created new MeasurementsFile");
            measurementsFile.setMeasurementsMic1(measurementRows);
            logger.info("inserted measurementRows");
            measurementsFile.setId(id);
            logger.info("set id: "+id);
            measurementRepository.save(measurementsFile);
            logger.info("saved MeasurementsFile");
        } catch (IOException e) {
            throw new RuntimeException("fail to store excel data!");
        }
    }

When I print the trace of the exception I get the following: "org.springframework.orm.jpa.JpaSystemException: ids for this class must be manually assigned before calling save(): be.uantwerpen.ft.se.tutorial_backend.model.users.MeasurementsFile". Is .setId not what they mean?

My MeasurentsFile class:

@Entity
public class MeasurementsFile {
    @Id
    private Long id;
    @Column
    @OneToMany
    private List<MeasurementRow> measurementsMic1;
    @Column
    @OneToMany
    private List<MeasurementRow> measurementsMic2;//when the setup has 2 mics, a second input file is uploaded
    @Column
    private double height;
    @Column
    private int[] referenceSpeeds; //passengercar and heavy vehicles
    @Column
    private boolean hasSecondaryMic;
    @Column
    private boolean hasBackingboard;
    //columnIDs and their default values
    private int numberColumnID=0;
    private int dateColumnID=0;
    private int timeColumnID=0;
    private int vehicleColumnID=0;
    private int speedColumnID=0;
    private int airTempColumnID=0;
    private int roadTempColumnID=0;//(optional)
    private int windSpeedColumnID=0;//(optional)
    private int laMaxColumnID=0;
    private int laMaxArrayColumnID=0;//26 elements by default (optional)
}

(I left out the imports and getter and setter functions since they're just autogenerated)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    You say that you get message - `could not upload...`. Looking at the code, this does not mean that `save()` does nothing, but that you are actually swallowing an exception thrown somewhere in the service (the `catch` clause for `Exception`). That's why generally it's bad idea to catch an `Exception`. Print the stack trace and edit question to include it, people won't be able to help without it. – Chaosfire Mar 26 '23 at 10:41
  • Sorry, you're right. here's the exception: "org.springframework.orm.jpa.JpaSystemException: ids for this class must be manually assigned before calling save(): be.uantwerpen.ft.se.tutorial_backend.model.users.MeasurementsFile" – hendrikvalcke Mar 26 '23 at 10:56
  • Looks like you have not specified generation strategy for the id. Maybe [Hibernate error: ids for this class must be manually assigned before calling save():](https://stackoverflow.com/questions/10997494/hibernate-error-ids-for-this-class-must-be-manually-assigned-before-calling-sav) will help. – Chaosfire Mar 26 '23 at 11:16
  • Don't set the id to 0. Try 1 instead. It is just for testing however. After that you should use a generated value. – Mar-Z Mar 26 '23 at 12:54

0 Answers0