我有两个表'Task'和'Contents'。我使用带有鉴别器的单表继承将两个实体(PrimaryContent,AttachmentContent)映射到'Contents'表。我有一个父实体Task,它与两个内容实体保持一对一的关系。我可以插入Task实体及其PrimaryContents和AttachmentContents的集合。我看到hibernate在每种类型的鉴别器列中插入了正确的值。问题是当我查询Task实体及其内容实体的集合时,hibernate会抛出异常 - >引起:org.hibernate.WrongClassException:id为10024的对象不是指定的子类:com.xxx.PrimaryContent(加载的对象是类com.xxx.AttachmentContent错误的类)。看起来似乎没有在hibernate生成的select语句中使用鉴别器列。
在这里,您可以看到任务实体根据其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 <= ?
以下是其中一个集合(PrimaryContent实体)的查询:
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=?
如您所见,where子句中没有使用鉴别器列。这只是我的实体映射的问题吗?我在下面列出了表格和orm映射:
表:
Tasks:
+id (pk)
+other fields
contents:
+id (pk)
+task_id (fk)
+primary_content_id (fk) <-- the self association
+segment_type <-- my discriminator column
任务:
<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>
内容基础:
<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>
主要内容:
<?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>
附件内容:
<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>
答案 0 :(得分:0)
在你的orm.xml中,你的鉴别器列为:
<discriminator-column name="COMM_SGMNT_TYPE_CD"/>
但是在您的表定义中,您将其称为segment_type