Search code examples
postgresqlhibernatejpa

Mapping array with Hibernate


Can you please help me to map this class using Hibernate?

public class MyClass{
    private Long id;
    private String name;
    private int[] values;
    ...
}

I'm using PostgreSQL and the column type in the table is integer[] How my array should be mapped?


Solution

  • I have never mapped arrays to hibernate. I always use collections. So, I have slightly changed you class:

    public class MyClass{
        private Long id;
        private String name;
        private List<Integer> values;
    
        @Id
        // this is only if your id is really auto generated
        @GeneratedValue(strategy=GenerationType.AUTO) 
        public Long getId() {
            return id;
        }
    
        @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
        public List<Integer> getValues() {
            return values;
        }   
        ...