I'm having a problem with a CSV data import code into the database. I'm using Java 11 with SpringBoot 2.7.3 REST API
I have a Report class that has a Many-To-Many one-way relationship with the SensorError class, where only Report knows about SensorError and not the other way around:
@ManyToMany @JoinTable( name = "rel_report__sensor_error", joinColumns = @JoinColumn(name = "report_id"), inverseJoinColumns = @JoinColumn(name = "sensor_error_id") ) private Set<SensorError> sensorErrors = new HashSet<>();
I created a code to import CSV data:
public static List<Report> csvToReports(InputStream is, ReportRepository reportRepository,
SensorErrorRepository sensorErrorRepository) {
try (BufferedReader fileReader = new BufferedReader(new InputStreamReader(is, Charset.defaultCharset()));
CSVParser csvParser = new CSVParser(fileReader,
CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());) {
// Create an empty list to hold the SiraReports that will be parsed from the CSV
// file
List<Report> reports = new ArrayList<Report>();
// Parse the SensorError objects from the CSVRecord
Set<SensorError> sensorErrors = new HashSet<>();
// Get an Iterable of CSVRecords from the CSVParser
Iterable<CSVRecord> csvRecords = csvParser.getRecords();
for (CSVRecord csvRecord : csvRecords) {
Report report = new Report(csvRecord.get("final_state"), Instant.parse(csvRecord.get("datetime_begin")),
Instant.parse(csvRecord.get("datetime_end")), csvRecord.get("cutting_time"),
csvRecord.get("total_time"));
// Check if sensor_name, error_code and error_detail fields have values
if (!csvRecord.get("sensor_name").isEmpty() || !csvRecord.get("error_code").isEmpty()
|| !csvRecord.get("error_detail").isEmpty()) {
// Create a new SensorError object using the data from the CSVRecord
SensorError sensorError = new SensorError(csvRecord.get("sensor_name"), csvRecord.get("error_code"),
csvRecord.get("error_detail"));
// Add the SensorError object to the Set
sensorErrors.add(sensorError);
}
// Set the SensorError objects for the SiraReport object
report.setSensorErrors(sensorErrors);
// Save the SensorError objects to the database using the SensorErrorRepository
sensorErrorRepository.saveAll(sensorErrors);
// Add the Report object to the List
reports.add(report);
}
// Return the List of Reports
return reports;
} catch (IOException e) {
throw new RuntimeException("Failed to parse CSV file: " + e.getMessage());
}
}
This code works partially for my interest, in my CSV file:
final_state,datetime_begin,datetime_end,cutting_time,total_time,sensor_name,error_code,error_detail
TESTESENSOR,2022-09-30T18:15:00Z,2022-09-30T18:35:00Z,30min,40min, "sensor operador","operando","falha parcial","sensor navalha","erro mundial","falha total"
The code saves only the first SensorError Information inside a Report: ""sensor operador","operando","falha parcial"", but not the second information: ""sensor navalha","erro mundial","falha total"".
I already tried some things, like adding: Set sensorErrors = new HashSet<>();, inside the Loop, but then it saves the second SensorError Information and not the first one anymore. I also tried changing it to a List instead of Set, but it didn't solve the problem.
How to make it import more than one existing SensorError information in the CSV file to a single SiraReport?
Report
intoreport
, but is never using that variable (but for setting some error on it) - there is asiraReport
being added toreports
, but its origin is not posted