Andna
Andna

Reputation: 6689

JPA - property that is a collection

I am using Hibernate as JPA provider. I have for example this class:

@Question("multipleChoiceQuestion")
@Entity
public class MultipleChoiceQuestion extends QuestionUnit {

    private ArrayList<String> questionContent;

    public MultipleChoiceQuestion() {
        this(null, null);
    }

    public MultipleChoiceQuestion(ArrayList<String> questionContent,
            AnswerUnit correctAnswer) {
        super(correctAnswer);
        this.questionContent = questionContent;
    }

    public ArrayList<String> getQuestionContent() {
        return this.questionContent;
    }
}

and if I persist it, the questionContent property is saved as blob. I know that I could use ElementCollection annotation to create seperate table for the contents of my ArrayList.

I am wondering though if this behaviour (saving collection that is a property as a blob) is specified somewhere in JPA specification or is it Hibernate specific?

Upvotes: 3

Views: 150

Answers (1)

Tiago Farias
Tiago Farias

Reputation: 3407

I've seen this behaviour at JPA, so it's not Hibernate specific. The thing is that, since you have no annotation for that attribute, and it's an ArrayList of primitives (so no ids) and ArrayList is Serializable, JPA engine "understands" that attribute should me mapped as a BLOB. Obviously the concept of string is not really an object, therefore, it would not make sense to map a string as an object in your ORM. I found a very good post about the subject.

In the item of hibernate specification - "2.2.2.5. Non-annotated property defaults" (I know, it's not JPA. But Hibernate follows it):

If a property is not annotated, the following rules apply:

Otherwise, if the type of the property is Serializable, it is mapped as @Basic in a column holding the object in its serialized version;

Upvotes: 3

Related Questions