JPA映射类(openjpa)

时间:2010-10-28 13:11:22

标签: java jpa

假设我有一个Question和QuestionHeader类。问题扩展了QuestionHeader。我的映射看起来像这样:

<entity class="hr.leads.services.model.jpa.QuestionHeader">
    <table name="question" />
    <inheritance strategy="SINGLE_TABLE" />
    <attributes>
        <basic name="description">
            <column name="description" length="200" nullable="false" />
        </basic>
        <basic name="text">
            <column name="text" length="300" nullable="false" />
        </basic>
        <many-to-one name="band"
            target-entity="hr.leads.services.model.jpa.Band" fetch="EAGER">
            <join-column name="band_id" />
        </many-to-one>
    </attributes>
</entity>

<entity class="hr.leads.services.model.jpa.Question">
    <table name="question" />
    <inheritance strategy="SINGLE_TABLE" />
    <attributes>
        <basic name="visibilityQuestionId">
            <column name="visibility_question_id" nullable="true" />
        </basic>
        <basic name="visibilityQuestionOptionId">
            <column name="visibility_question_option_id"
                nullable="true" />
        </basic>
        <many-to-one name="questionType"
            target-entity="hr.leads.services.model.jpa.QuestionType"
            fetch="EAGER">
            <join-column name="question_type_id" />
        </many-to-one>
        <one-to-many name="options"
            target-entity="hr.leads.services.model.jpa.QuestionOption"
            fetch="EAGER" mapped-by="question">
            <cascade>
                <cascade-all />
            </cascade>
        </one-to-many>
    </attributes>
</entity>

问题是,当我尝试添加实体时,这里是生成的SQL:

INSERT INTO question (name, display_order, description, text, DTYPE, band_id, visibility_question_id, visibility_question_option_id, question_type_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [params=(String) q1, (int) 1, (String) d1, (String) t, (String) QUESTION, (long) 101, (long) 42, (long) 43, (long) 101]} 

它将DTYPE作为列名,我不希望它这样做!我该怎样预防呢?我认为这是鉴别器值,但我不想在插入语句中使用它。

1 个答案:

答案 0 :(得分:3)

当使用SINGLE_TABLE策略时,需要使用鉴别器列来确定实体属于哪个子类型。可以重命名并决定每个类存储哪个值。

如果您对此不满意,可以选择其他两种策略。

  • 每班表
  • 加入

有关详细信息,请参阅documentation