I’m encountering an error while saving a Department object with Spring Data R2DBC. My Department model has an employees property that can hold a list of Employee objects. However, when I try to create a new department with an empty employee list, I get the following error:
java.lang.IllegalArgumentException: Unsupported array type: io.spring.graphql.employeedemo2.model.Employee
Code Snippet:
Department.class
@Data
@NoArgsConstructor
@Table(name = "department_tbl")
public class Department {
@Id
@Column("department_name")
private int id;
@Column("department_name")
private String name;
private List<Employee> employees;
public Department(int id, String name, List<Employee> employees){
this.id = id;
this.name = name;
this.employees = new ArrayList<>();
}
AddDepartmentInput.class
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AddDepartmentInput {
private int id;
private String name;
}
@MutationMapping
public Mono<Department> createDepartment(@Argument AddDepartmentInput departmentInput) {
return Mono.just(departmentInput)
.map(dptInput -> {
Department department = new Department();
department.setId(dptInput.getId());
department.setName(dptInput.getName());
department.setEmployees(new ArrayList<>()); // Empty list initialization
return department;
})
.flatMap(department -> departmentRepository.save(department));
}
and this is my schema.graphqls
type Employee {
id: ID!
name: String
salary: Float
departmentId: ID!
}
type Department {
id: ID!
name: String
employees: [Employee]
}
type Mutation {
createDepartment(departmentInput: AddDepartmentInput ):Department
}
input AddDepartmentInput {
id:ID!
name:String
}
Context:
- I’m using Spring Data R2DBC for data persistence.
- I want to allow creating departments initially without any associated employees.
Question:
How can I effectively save a Department object with an empty employees
list using Spring Data R2DBC? Are there recommended approaches for handling empty lists within models?