Search code examples
javahibernatejpaplayframework

JPA Array Mapping


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];
    }
}

Solution

  • 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.