繁体   English   中英

Hibernate一对多映射

[英]Hibernate one to many mapping

我有两张桌子:

<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]

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

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

谢谢。

UPDATE

Okey,修改为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])

这个错误意味着我必须有一个复合主键用于secretquestions表我不想要:(

UPDATE

我将举一个例子来更清楚我想做的事情:

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。

看起来这种方法无法工作且Hibernate不支持(Content表有复合主键,而我想将它映射到Question表中的一个字段),因此我使用了一个解决方法,我只将问题映射到contentID我使用ContentGetter类来获取内容取决于languageID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM