0

I have a table which contains a column of type: integer[]

I'm trying to map my entity to this table and I've tried the following suggestion of:

@ElementCollection
private ArrayList<Integer> col;

public MyEntity() {
  col = new ArrayList<>();
}

However I get the following error: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements

Not sure how to get around this. I'm open to changing the entity's datatype, but I would prefer not to move this property into its own table/entity. Is there another solution? Thanks.

user1491636
  • 2,355
  • 11
  • 44
  • 71

1 Answers1

0

The field must be of type List<Integer>, not ArrayList<Integer>.

The JPA engine must be able to use its own List implementation, used for lazy-loading, dirty checking, etc.

It's a good idea in general to program on interfaces rather than implementations, and it's a requirement to do it in JPA entities.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • That's actually how I had it originally, but I got a different error. I saw in another post that it was recommended to switch to a concrete implementation of a List. Anyways, using `List` I get the following error: `ERROR: relation "myentity_col" does not exist`. Don't know why its even looking for such a relation. – user1491636 Apr 22 '14 at 15:57
  • Because you need a database table to store these integers. Since you didn't provide any CollectionTable annotation to specify the table name, the default name (`myentity_col`) is used – JB Nizet Apr 22 '14 at 16:09
  • The integers are stored in the same table as the entity. Its an integer[] column datatype. – user1491636 Apr 22 '14 at 17:05
  • 1
    That is a non-standard way of mapping a list of integers. ElementCollection won't help. You'll need to define a custom type (if using Hibernate, don't know for other implementations). – JB Nizet Apr 22 '14 at 17:21