Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Can you please help me to map th class Hbernate?

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

//omitting getters and setters
}

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

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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
@GeneratedValue(strategy=GenerationType.AUTO) // this is only if your id is really auto generated
public Long getId() {
return id;
}

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
public List<Integer> getValues() {
    return values;
}   
share|improve this answer

Hibernate (and JPA) can't directly map the PostgreSQL array type. See this question for how to proceed if you really need to retain your database structure as it is. This thread has an example of the required custom type.

If you can change your schema, you can let hibernate create an additional table to handle the collection - List<Integer>. Then, depending on the version of hibernate you are using:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.