JPA array mapping

How can I match an array of paired numbers in JPA. I have the following code that does not work because hibernation cannot initialize 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];
    }
}
+5
source share
2 answers

JPA does not guarantee the ability to store arrays in a separate table; obviously JDO does, but then you decided not to use this. Therefore, you need to either save them as @Lob, or change your Java type to a list.

+10
source

Use an object type like ArrayList. Example

@ElementCollection
public ArrayList<Double> values;

public YearlyTarget(int year) {
    this.year = year;
    this.values = new ArrayList<Double>(12);
}
+7
source

All Articles