danny.lesnik danny.lesnik - 5 months ago 25
Java Question

Mapping array with Hibernate

Can you please help me to map th class Hbernate?

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


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

Answer

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;
    }   
    ...
Comments