I have a problem how approach to convert List<Map<String, String>>
to custom class objects to save it to JPA. If I apply the branchRepository.save(content)
it does not work.
Here's my code:
-- BranchService.java --
public List<Map<String, String>> uploadEmployee(MultipartFile multip) throws Exception {
String fileNames = multip.getOriginalFilename();
DataFormatter formatter = new DataFormatter();
File file = new File("./reports/" + fileNames);
Workbook workbook = WorkbookFactory.create(file);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheetAt(0);
int headerRowNum = sheet.getFirstRowNum();
Map<Integer, String> colHeaders = new HashMap<Integer, String>();
Row row = sheet.getRow(headerRowNum);
for (Cell cell : row) {
int colIdx = cell.getColumnIndex();
String value = formatter.formatCellValue(cell, evaluator);
colHeaders.put(colIdx, value);
}
List<Map<String, String>> content = new ArrayList<Map<String, String>>();
for (int r = headerRowNum + 1; r <= sheet.getLastRowNum(); r++) {
row = sheet.getRow(r);
if (row == null)
row = sheet.createRow(r);
Map<String, String> valuesToHeaders = new HashMap<String, String>();
for (Map.Entry<Integer, String> entry : colHeaders.entrySet()) {
int colIdx = entry.getKey();
Cell cell = row.getCell(colIdx);
if (cell == null)
cell = row.createCell(colIdx);
String cellValue = formatter.formatCellValue(cell, evaluator);
valuesToHeaders.put(entry.getValue(), cellValue);
}
content.add(valuesToHeaders);
}
workbook.close();
System.out.println(content);
return content;
}
How to convert and apply it to JPA?