开发者

JPA - single table inheritance with discriminator inserts but fails to retrieve correct object

开发者 https://www.devze.com 2023-03-22 20:37 出处:网络
I have two tables \'Task\' and \'Contents\'. Im using single table inheritance with discriminator to map two entities (PrimaryContent, AttachmentContent) to the \'Contents\' table. I have a parent ent

I have two tables 'Task' and 'Contents'. Im using single table inheritance with discriminator to map two entities (PrimaryContent, AttachmentContent) to the 'Contents' table. I have a parent entity Task that holds one-to-may relationships to the two contents en开发者_运维问答tities. I can insert the Task entity and it's collections of PrimaryContents and AttachmentContents. I see hibernate is inserting the correct value in the discriminator columns for each type. The issue is when I query for the Task entity and its collections of content entities, hibernate throws an exception --> Caused by: org.hibernate.WrongClassException: Object with id: 10024 was not of the specified subclass: com.xxx.PrimaryContent (loaded object was of wrong class class com.xxx.AttachmentContent). It looks as if the discriminator column isn't getting used in the hibernate generated select statement.

Here you can see the Task entity is queried correctly based on its pk:

Hibernate: select * from ( select communicat0_.COMM_TASK_ID as COMM1_92_,    communicat0_.BUSINESS_CONTEXT as BUSINESS2_92_, communicat0_.BUSINESS_ID as BUSINESS3_92_, communicat0_.CREATE_DT as CREATE4_92_, communicat0_.STAT_RSN_CD as STAT5_92_, communicat0_.SENDERS_EMAIL_ID as SENDERS6_92_, communicat0_.SENDERS_NM as SENDERS7_92_, communicat0_.STATUS_DT as STATUS8_92_, communicat0_.STATUS_CD as STATUS9_92_, communicat0_.SOURCE_SYSTEM as SOURCE10_92_, communicat0_.TRANSACTION_REFERENCE_ID as TRANSAC11_92_, communicat0_.UPDATE_DT as UPDATE12_92_, communicat0_.USER_ID as USER13_92_  from COMMPREF.COMMUNICATION_TASKS communicat0_ where communicat0_.COMM_TASK_ID=10029 ) where rownum <= ?

Here is the query for one of its collections (the PrimaryContent entity):

Hibernate: select primarycon0_.COMM_TASK_ID as COMM24_92_1_, primarycon0_.COMM_CONTENT_ID as COMM2_1_, primarycon0_.COMM_CONTENT_ID as COMM2_93_0_, primarycon0_.ARCHIVE_DT as ARCHIVE3_93_0_, primarycon0_.ARCHIVE_REF_ID as ARCHIVE4_93_0_, primarycon0_.FILE_NAME as FILE5_93_0_, primarycon0_.FILE_PATH as FILE6_93_0_, primarycon0_.FORMAT_TYPE_CD as FORMAT7_93_0_, primarycon0_.CONTENT_NM as CONTENT8_93_0_, primarycon0_.PAGE_COUNT as PAGE9_93_0_, primarycon0_.COMM_CAT_CD as COMM10_93_0_, primarycon0_.EDIT_YN as EDIT11_93_0_, primarycon0_.FILE_LOCKED_BY as FILE12_93_0_, primarycon0_.FILE_LOCKED_DT as FILE13_93_0_, primarycon0_.PREVIEW_YN as PREVIEW14_93_0_, primarycon0_.STAT_RSN_CD as STAT15_93_0_, primarycon0_.STATUS_DT as STATUS16_93_0_, primarycon0_.STATUS_CD as STATUS17_93_0_, primarycon0_.COMM_TASK_ID as COMM24_93_0_, primarycon0_.TEMPLATE_ID as TEMPLATE18_93_0_, primarycon0_.TEMPLATE_NM as TEMPLATE19_93_0_, primarycon0_.TEMPLATE_VERSION as TEMPLATE20_93_0_, primarycon0_.USER_ID as USER21_93_0_ from COMMPREF.COMM_CONTENTS primarycon0_ where primarycon0_.COMM_TASK_ID=?

As you can see, there is no discriminator column used in the where clause. Is this simply an issue with my entity mappings? I have included tables and the orm mapping below:

Tables:

Tasks:
+id (pk) 
+other fields

contents:
+id (pk)
+task_id (fk)
+primary_content_id (fk) <-- the self association 
+segment_type <-- my discriminator column

Task:

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
 http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
    version="2.0">
    <description></description>
    <package></package>
    <schema></schema>
    <access>FIELD</access>
    <entity class="com.xxx.CommunicationTask"
        access="FIELD" metadata-complete="true">
        <table name="COMMUNICATION_TASKS" />
        <attributes>
            <id name="id">
                <column name="COMM_TASK_ID" />
                <generated-value strategy="SEQUENCE" generator="TASK_SEQ"  />
                <sequence-generator name="TASK_SEQ"
                    sequence-name="COMMPREF.TASK_SEQ" allocation-size="1" />
            </id>
            <one-to-many name="attachmentContents" target-   entity="com.xxx.AttachmentContent" 
                mapped-by="task" orphan-removal="true">
                <cascade>
                    <cascade-all />
                </cascade>
            </one-to-many>
        <one-to-many name="primaryContents" target-entity="com.xxx.PrimaryContent" 
                mapped-by="task" orphan-removal="true">
                <cascade>
                    <cascade-all />
                </cascade>
        </one-to-many>
        </attributes>
    </entity>
  </entity-mappings>

content base:

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
  http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
    version="2.0">
    <description></description>
    <package></package>
    <schema></schema>
    <access>FIELD</access>
    <entity name="CommunicationContent" class="com.xxx.CommunicationContent"  access="FIELD" metadata-complete="true">
    <table name="COMM_CONTENTS"/> 
    <discriminator-column name="COMM_SGMNT_TYPE_CD"/>
    <attributes>
        <id name="id">
            <column name="COMM_CONTENT_ID"/>
            <generated-value strategy="SEQUENCE" generator="CONTENT_SEQ"/>
            <sequence-generator name="CONTENT_SEQ" sequence-name="COMMPREF.CONTENT_SEQ"     allocation-size="1"/>
        </id>
    </attributes>
 </entity>
 </entity-mappings>

primary content:

<?xml version="1.0" encoding="UTF-8"?>
 <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
   http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
    version="2.0">
    <description></description>
    <package></package>
    <schema></schema>
    <access>FIELD</access>
    <entity name="PrimaryContent" class="com.xxx.PrimaryContent"
        access="FIELD" metadata-complete="true">
        <discriminator-value>P</discriminator-value>
        <attributes>
            <many-to-one name="task" fetch="LAZY">
                <join-column name="COMM_TASK_ID" nullable="false"/>
            </many-to-one>
        </attributes>
    </entity>
 </entity-mappings>

attachment content:

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
   http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
    version="2.0">
    <description></description>
    <package></package>
    <schema></schema>
    <access>FIELD</access>
    <entity name="AttachmentContent" class="com.xxx.AttachmentContent"
        access="FIELD" metadata-complete="true">
        <discriminator-value>A</discriminator-value>
        <attributes>
            <many-to-one name="task" fetch="LAZY">
                <join-column name="COMM_TASK_ID" nullable="false"/>
            </many-to-one>
        </attributes>
    </entity>
 </entity-mappings>


In your orm.xml you have your discriminator column as:

<discriminator-column name="COMM_SGMNT_TYPE_CD"/>

But in your table definition you have it called segment_type

0

精彩评论

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