3

我使用 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 的内容创建单独的表。

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

4

1 回答 1

3

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

休眠规范的项目中-“ 2.2.2.5. Non-annotated property defaults ”(我知道,它不是 JPA。但是 Hibernate 遵循它):

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

否则,如果属性的类型是可序列化的,则将其映射为@Basic 在保存序列化版本中的对象的列中;

于 2012-08-14T20:35:31.620 回答