2012-08-14 63 views
3

我使用Hibernate作爲JPA提供程序。我有這個例子類: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特定的?

回答

3

我在JPA上看到過這種行爲,所以它不是Hibernate特有的。問題是,既然你沒有註解那個屬性,並且它是一個基元的ArrayList(所以沒有ID),並且ArrayList是Serializable,JPA引擎會「理解」該屬性應該映射爲BLOB。 很顯然,字符串的概念並不是真正的對象,因此,在您的ORM中將字符串映射爲對象是沒有意義的。關於這個問題,我發現了一個很好的post

hibernate specification項目 - 「2.2.2.5未標註屬性默認」(我知道,這不是JPA但休眠緊隨其後):

如果屬性沒有被標註,以下規則適用:

否則,如果屬性的類型是可序列化的,則在保存該序列化版本中的對象的列中,將其映射爲 作爲@Basic;

+0

好的,謝謝你的回答。 – Andna 2012-08-15 09:04:57