Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map an integer array in JPA

I use spring jpa with hibernate and postgres

In an entity, I tried to use a List and integer[]

In the database, I have a column of type:

integer[] 

is there any jpa way to use it?

like image 535
robert trudel Avatar asked Sep 08 '17 12:09

robert trudel


2 Answers

JPA is not able to persist arrays to a separate table or database array (e.g. ones mapped to java.sql.Array) out of the box. So you have two ways:

1) Use @Lob to save this column as BLOB or CLOB

@Lob
private Integer[] values;

2) Use List<Integer> instead of an array

@ElementCollection
public List<Integer> values;
like image 173
Mykola Yashchenko Avatar answered Nov 13 '22 07:11

Mykola Yashchenko


Just doing this worked fine for me:

@Column(name="column_name")
private Integer[] columnName;

I didn't need @Lob like Mykola Yashchenko suggested.

like image 30
Grant Foster Avatar answered Nov 13 '22 08:11

Grant Foster