How can I map an array of Doubles in JPA. I have the following code which fails because hibernate cannot initialise the array.
@Entity
public class YearlyTarget extends GenericModel {
@Id
public Integer year;
@ElementCollection
public Double[] values;
public YearlyTarget(int year) {
this.year = year;
this.values = new Double[12];
}
}
JPA does not mandate being able to persist arrays to a separate table; obviously JDO does but then you have chosen not to use that. Consequently you need to either store them as @Lob, or change your java type to a List.
You don't specify the required database structure backing your mapping.
@ElementCollection
relies on a table that is joined up on retrieving the collection.In Postgresql database for example you are able to store a simple array in within a column which is possible to map. You will need to in include a dependency:
And your entity definition will look like:
Currently the library only supports ints and strings, but it is a fairly simple task to add new types.
Use an Object type, such as ArrayList. Example