Issue
I am receiving a simple list of values part of JSON request which I want to save as comma separated values. Tried using following but it did not work.
@Column(nullable = true)
@GeneratedValue(strategy = GenerationType.AUTO)
private ArrayList<String> services = new ArrayList<String>() ;
and
@Column(nullable = true)
@ElementCollection(targetClass = String.class)
private List<String> services = new ArrayList<String>() ;
@ElementCollection
threw an exception saying table services does not exist
.
Solution
The @ElementCollection requires a table to store multiples rows of values,
So you could define as a String column and join/explode in getters and setters, like this
private String services;
public setServices(String services[]) //Can be Array or List
{
// this.services = Iterate services[] and create a comma separated string or Use ArrayUtils
}
public String[] getServices() //Can be Array or List
{
// services.split(",") to get a list of Strings, then typecast/parse them to Strings before returning or use Arrays.asList(arguments.split(","));
}
Answered By - Túlio Castro
Answer Checked By - Mildred Charles (JavaFixing Admin)