NHibernate中一对一映射的困惑 [英] Confusion over one-to-one mapping in NHibernate

查看:102
本文介绍了NHibernate中一对一映射的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我建议在以下情况下使用的适当映射:

Can someone suggest to me the appropriate Mapping to be used in the following situation:

父表-AllTransactions

Parent Table - AllTransactions

子表-TranCategory1,TranCategory2和TranCategory3以及另外8个.

Child Table - TranCategory1, TranCategory2 and TranCategory3 and 8 more.

所有十二个表共享相同的组合键.现在,AllTransactions中的计数是所有子表中事务的总和.子表仅包含父表中的唯一值.

All twelve tables share the same composite keys. Now count in AllTransactions is the sum of transactions in all the child tables. The child tables contain only unique values from the parent table.

我正在使用的映射如下所示.

The mapping that I am using is shown below.

我在父表中使用constrained ="true"只是为了避免在父表的Select语句中形成联接.但是我想我违背了约束"一词的定义,该词规定了AllTransactions中的每个事务都必须在TranCategory1中有一个事务.但是实际上,在我的设计中情况恰恰相反. 那么,如何修改映射以复制正确的行为并避免使用联接?

I am using constrained="true" in the Parent table only to avoid forming joins in the Select statement for the Parent table. But I guess I am going against the definition of the word 'constrained' which specifies that for every transaction in AllTransactions there must be a transaction in TranCategory1. But actually the reverse is true in my design. So how do I modify my mappings to replicate the correct behavior and also avoid using of joins?

更新:加入父子表时,我当前的映射使我出错.请租约帮助我修改映射,以便可以对上述表进行联接.

Update: My current mapping is throwing me errors while Joining the Parent and Child table.lease help me modify the mappings so that I can perform a join on the above tables.

完整映射

Parent Table Mapping

<composite-id name="Key" class="AccountEventKey">
  <key-property name="TransactionId" column="TRANSACTION_ID" type="long"/>
  <key-property name="PartitionMonthNum" column="PARTITION_MONTH_NUM" type ="int"/>
</composite-id>

<many-to-one name="RefAuditQueue" class="RefAuditQueue">
  <column name="AUDIT_QUEUE_ID" not-null ="true"/>
</many-to-one>

<many-to-one name="RefAuditRule" class="RefAuditRules">
  <column name="AUDIT_RULE_ID" not-null ="false"/>
</many-to-one>

<many-to-one name="AccountDailyInfo" class="AccountDailyInfo">
  <column name="ACCOUNT_ID" length="19" not-null ="false"/>
  <column name="ACCOUNT_INFO_PROCESSING_DATE" not-null="false"/>
</many-to-one>

<one-to-one name="DisclosureGroupChange" class="DisclosureGroupChanges" constrained="true"/>

<bag name="AuditReviewAssignmentList" table="AUDIT_REVIEW_ASSIGNMENT" generic="true" inverse="true" cascade="save-update" lazy="true" >
  <key>
    <column name="TRANSACTION_ID" sql-type="long" not-null ="true"/>
    <column name="PARTITION_MONTH_NUM" sql-type="int" not-null ="true"/>
  </key>
  <one-to-many class="AuditReviewAssignment"/>
</bag>

<property name="RacfId" type="string" not-null="false">
  <column name="RACFID" length="7"/>
</property>

<property name="EmployeeDailyAssignmentKey" type="long" not-null="false">
  <column name="EMP_DAILY_ASSIGNMENT_KEY"/>
</property>

<property name="AuditReviewRequiredInd" type="YesNo" not-null="false">
  <column name="AUDIT_REVIEW_REQUIRED_IND"/>
</property>

<property name="TransactionAmount" type="long" not-null="false">
  <column name="TRANSACTION_AMOUNT"/>
</property>

<property name="TransactionDate" type="Timestamp" not-null="false">
  <column name="TRANSACTION_DATE"/>
</property>

<property name="LastModifiedDate" type="Date" not-null="false">
  <column name="LAST_MODIFIED_DATE"/>
</property>

<property name="FirstAddedDate" type="Date" not-null="false">
  <column name="FIRST_ADDED_DATE"/>
</property>

<property name="AccountReferenceNum" type="string" not-null="false">
  <column name="ACCOUNT_REFERENCE_NUM" length="50"/>
</property>

<property name="CcaEtlRunNum" type="long" not-null="true">
  <column name="CCA_ETL_RUN_NUM"/>
</property>

1 Child Table Mapping (There are 10 more child tables)

<composite-id name="Key" class="AccountEventKey">
  <key-property name="TransactionId" column="TRANSACTION_ID" type="long"/>
  <key-property name="PartitionMonthNum" column="PARTITION_MONTH_NUM" type ="int"/>
</composite-id>

<many-to-one name="AccountEvent" class="AccountEvent" insert="false" update="false" unique="true">
  <column name="TRANSACTION_ID"/>
  <column name="PARTITION_MONTH_NUM"/>
</many-to-one>    

<property name="FrDisclosureGrpName" type="string" not-null="false">
  <column name="FR_DISCLOSURE_GRP_NAME" length="8"/>
</property>

<property name="ToDisclosureGrpName" type="string" not-null="false">
  <column name="TO_DISCLOSURE_GRP_NAME" length="8"/>
</property>

<property name="LastModifiedDate" type="Date" not-null="false">
  <column name="LAST_MODIFIED_DATE=" />
</property>

<property name="FirstAddedDate" type="Date" not-null="false">
  <column name="FIRST_ADDED_DATE" />
</property>

<property name="CcaEtlRunNum" type="long" not-null="true">
  <column name="CCA_ETL_RUN_NUM" />
</property>

联接查询导致错误

var query1 = _ses.CreateCriteria<AccountEvent>().CreateAlias("DisclosureGroupChange", "discGrp", NHibernate.SqlCommand.JoinType.InnerJoin).List<AccountEvent>()

该异常消息是:

"ORA-00923:在期望的位置找不到FROM关键字\ n"

"ORA-00923: FROM keyword not found where expected\n"

异常消息中显示的SQL语句

The SQL statement shown in the exception message

SELECT this_.TRANSACTION_ID as TRANSACT1_15_1_, this_.PARTITION_MONTH_NUM as PARTITION2_15_1_, this_.AUDIT_QUEUE_ID as AUDIT3_15_1_, this_.AUDIT_RULE_ID as AUDIT4_15_1_, this_.ACCOUNT_ID as ACCOUNT5_15_1_, this_.ACCOUNT_INFO_PROCESSING_DATE as ACCOUNT6_15_1_, this_.RACFID as RACFID15_1_, this_.EMP_DAILY_ASSIGNMENT_KEY as EMP8_15_1_, this_.AUDIT_REVIEW_REQUIRED_IND as AUDIT9_15_1_, this_.TRANSACTION_AMOUNT as TRANSAC10_15_1_, this_.TRANSACTION_DATE as TRANSAC11_15_1_, this_.LAST_MODIFIED_DATE as LAST12_15_1_, this_.FIRST_ADDED_DATE as FIRST13_15_1_, this_.ACCOUNT_REFERENCE_NUM as ACCOUNT14_15_1_, this_.CCA_ETL_RUN_NUM as CCA15_15_1_, discgrp1_.TRANSACTION_ID as TRANSACT1_52_0_, discgrp1_.PARTITION_MONTH_NUM as PARTITION2_52_0_, discgrp1_.FR_DISCLOSURE_GRP_NAME as FR3_52_0_, discgrp1_.TO_DISCLOSURE_GRP_NAME as TO4_52_0_, discgrp1_.LAST_MODIFIED_DATE= as LAST5_52_0_, discgrp1_.FIRST_ADDED_DATE as FIRST6_52_0_, discgrp1_.CCA_ETL_RUN_NUM as CCA7_52_0_ FROM CCAPRD1O_AT.ACCOUNT_EVENT this_ inner join CCAPRD1O_AT.DISCLOSURE_GROUP_CHANGES discgrp1_ on this_.TRANSACTION_ID=discgrp1_.TRANSACTION_ID and this_.PARTITION_MONTH_NUM=discgrp1_.PARTITION_MONTH_NUM

好吧,我可以确认仅当我在AccountEvent表中添加DisclosureGroupChanges的映射时,此消息才会出现.首先,由于出现错误,我避免了这种映射.但是现在我需要在这些表之间执行联接,据我所知,我们只能对NHibernate中的相关表进行联接.如果我错了,请纠正我.

Well I can kind of confirm that this message only comes when I add the mapping for the DisclosureGroupChanges in the AccountEvent table. First I avoided this mapping as I was getting the error. But now I need to perform a Join between these tables and as per my knowledge we can do joins only for related tables in NHibernate. Please correct me if I am wrong here.

推荐答案

我犯了我犯的错误.

子表中的映射之一的值为"LAST_MODIFIED_DATE =".这总是导致我出错.

One of the mappings in the Child Table had the value as "LAST_MODIFIED_DATE=". This was causing me the error always.

这篇关于NHibernate中一对一映射的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆