【问题标题】:Hibernate one to many mapping休眠一对多映射
【发布时间】:2011-04-07 16:16:54
【问题描述】:

我有两张桌子:

<class name="Content" table="language_content" lazy="false">
    <composite-id>  
        <key-property name="contentID" column="contentID"/>  
        <key-property name="languageID" column="languageID"/>           
    </composite-id> 
    <property name="Content" column="Content" />
</class>

<class name="SecretQuestion" table="secretquestions" lazy="true">
    <id name="questionID" type="integer" column="QuestionID">
        <generator class="increment"></generator>
    </id>
    <property name="question" column="Question"/>
    <property name="isUsing" column="IsUsing"/>
    <bag name="contents" inverse="true" cascade="all,delete-orphan">
        <key column="question" />
        <one-to-many class="Content" />
    </bag>
</class>

我正在尝试将 SecretQuestion 的 "question" 属性 映射到 Content 的 "contentID" 属性,但 Eclipse 抛出异常:

org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.sms.model.entity.SecretQuestion.contents#1]

如果我将&lt;key column="question" /&gt;替换为&lt;key column="contentID" /&gt;,它可以运行但映射错误的数据(实际上,据我所知,它将questionID映射到Content的contentID)

任何人都知道如何做我想要在这里实现的目标吗?

谢谢。

更新

好的,按照Pascal所说的修改后,这是新的错误:

javax.servlet.ServletException: org.hibernate.MappingException: Foreign key (FKB65C9692FCD05581:language_content [contentID,languageID])) must have same number of columns as the referenced primary key (secretquestions [QuestionID])

这个错误意味着我必须有一个我不想要的秘密问题表的复合主键:(

更新

我将举一个例子来更清楚我想要做什么:

Table SecretQuestion
questionID* question answer
1           4        a
2           5        a
3           6        a

Table Content
contentID* languageID* content
1          1           a
1          2           b
2          1           c
2          2           d
3          1           e
3          2           f
4          1           g
4          2           h
5          1           i
5          2           j
6          1           k
6          2           l

现在我想将问题 4、5、6 映射到内容 ID 4、5、6。

【问题讨论】:

    标签: java hibernate orm mapping one-to-many


    【解决方案1】:

    看起来这种方法不起作用并且 Hibernate 不支持(内容表具有复合主键,而我只想将其映射到问题表中的一个字段),因此我使用了一种解决方法,我只映射问题到 contentID,我使用 ContentGetter 类来获取依赖于语言 ID 的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多