JPA Array Mapping

25,841

Solution 1

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.

Solution 2

Use an Object type, such as ArrayList. Example

@ElementCollection
public ArrayList<Double> values;

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

Solution 3

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:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
</dependency>

And your entity definition will look like:

@Entity
@Table(name = "products")
@TypeDefs(@TypeDef(name = "string-array", typeClass = StringArrayType.class))
public class Product {

    @Type(type = "string-array" )
    @Column(name = "colours")
    private String[] colours;

}

Currently the library only supports ints and strings, but it is a fairly simple task to add new types.

Share:
25,841
emt14
Author by

emt14

Software developer mainly focusing on Java stack.

Updated on July 09, 2022

Comments

  • emt14
    emt14 almost 2 years

    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];
        }
    }
    
  • emt14
    emt14 over 12 years
    Do you mean that arrays cannot be mapped directly with jpa and a collection needs to be used instead?
  • tmbrggmn
    tmbrggmn over 12 years
    @emt14 Plain arrays would be a real pain in the ass to work with, if you have a collection of values that changes frequently. This question tackles the same issue, with the same outcome: use a collection.
  • emt14
    emt14 over 12 years
    Arrays are also the best storage option for a fixed length data type. No overhead compared to collections. Using the collection seems to be a workaround the fact that jpa does not persists arrays.
  • ewernli
    ewernli over 12 years
    @emt14 I suspect JPA implementations can no proxy arrays as easily as lists or other collections.
  • Adriaan Koster
    Adriaan Koster almost 5 years
    @emt14 An ArrayList uses an array as underlying storage.There is not that much overhead when using an ArrayList instead of an array. Yes, resizing occurs but usually it does not have a huge performance impact (depends on the context of course). See also: stackoverflow.com/questions/716597/… The common opinion is that the benefits of using the List interface abstraction for readability and testability outweighs other concerns.