JPA - 属于集合的属性

时间:2012-08-14 17:17:09

标签: hibernate jpa

我正在使用Hibernate作为JPA提供程序。我有这样的课程:

@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;
    }
}

如果我坚持它,questionContent属性将保存为blob。我知道我可以使用ElementCollection注释为我的ArrayList的内容创建单独的表。

我想知道,如果在JPA规范中的某处指定了这种行为(将属性保存为blob的集合),还是特定于Hibernate?

1 个答案:

答案 0 :(得分:3)

我在JPA上看到过这种行为,所以它不是特定于Hibernate的。问题在于,由于您没有该属性的注释,并且它是基元的ArrayList(因此没有ID),而ArrayList是Serializable,因此JPA引擎“理解”该属性应该映射为BLOB。 显然字符串的概念实际上并不是一个对象,因此,将字符串映射为ORM中的对象是没有意义的。我找到了关于这个主题的非常好的post

hibernate specification - “ 2.2.2.5。非注释属性默认值”的项目中(我知道,它不是JPA。但是Hibernate会遵循它):

  

如果未注释属性,则适用以下规则:

     

否则,如果属性的类型是Serializable,则映射它   在包含序列化版本的对象的列中作为@Basic;