开发者

Hibernate one to many mapping

开发者 https://www.devze.com 2023-01-14 21:17 出处:网络
I have two tables: <class name=\"Content\" table=\"language_content\" lazy=\"false\"> <composite-id>

I have two tables:

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

and

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

I'm trying to map the "question" property of SecretQuestion to the "contentID" property of Content but Eclipse throwing out an exception:

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

If I replace the <key colu开发者_如何转开发mn="question" /> with <key column="contentID" />, it can run but mapping the wrong data (actually, as far as I see, it maps the questionID with the contentID of Content)

Anyone know how to do what I'm trying to achieve here?

Thanks.

UPDATE

Okey, after modifying as Pascal said, here is the new error:

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

This error means I have to have a composite primary key for secretquestions table which I don't want :(

UPDATE

I will give an example to clearer what I trying to do:

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

Now I want to map question 4, 5, 6 to content ID 4, 5, 6.


Looks like this approach cannot work and is not supported by Hibernate (the Content table have composite primary key while I want to mapping it to only one field in the Question table), thus I use a workaround that I only map the question to the contentID and I use a ContentGetter class that will get the content depend on the languageID.

0

精彩评论

暂无评论...
验证码 换一张
取 消