Opencsv building parent-child relationship while loading

Viewed 20

I am using Spring Boot multipart upload to load a CSV file. For now I am using Apache CSV to convert from CSV to preferred pojo objects.

This is how my sample CSV looks like:

enter image description here

Can I use opencsv which creates parent child objects dynamically for something like above data? Also, if I use opencsv, can I add any validations for each column just before converting?

  • Something like if:

column value == "ec2" then return "blah blah"

column value == "ec4" then return "null"

I have Projects and Employees. Each Project having multiple Employees.

My sample pojo's looks as follows:

Project Class which will be parent:

@Entity
@Table(name = "Projects")
@Getter
@setter
public class Project {
    private long id;
    private String projectCode;
    private String projectName;
    private String projectDescription;
    
@OneToMany(fetch = FetchType.LAZY, mappedBy = "project", cascade = CascadeType.PERSIST)
    private List<Employee> employess;
}

Employee class which will be child:

@Entity
@Table(name = "Employees")
@Getter
@Setter
public class Employee {
    private long id;
    private String empCode;
    private String empName;
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "project_id", nullable = false)
    @JsonBackReference
    private Project project;
}
0 Answers
Related