NHibernate - 每个子类继承的表导致“无效转换”例外情况
我有一个简单的每个子类表继承,具有以下 NHibernate 映射,
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default- cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="BillingDetail table="BillingDetails">
<id name="Id" type="System.Int32">
<column name="Id" />
<generator class="identity" />
</id>
<property name="DateAdded" type="System.DateTime">
<column name="DateAdded" />
</property>
<many-to-one class="Account name="Account">
<column name="Account_id" />
</many-to-one>
<joined-subclass name="BankAccount table="BillingDetails_BankAccount">
<key>
<column name="Id"/>
</key>
<property name="AccountNumber" type="System.Int64">
<column name="AccountNumber" />
</property>
<property name="SortCode" type="System.Int32">
<column name="SortCode" />
</property>
</joined-subclass>
<joined-subclass name="CreditCard table="BillingDetails_CreditCard">
<key>
<column name="Id" />
</key>
<property name="CardNumber" type="System.Int64">
<column name="CardNumber" />
</property>
<property name="CardType" type="System.String">
<column name="CardType" />
</property>
<property name="ExpiryDate" type="System.DateTime">
<column name="ExpiryDate" />
</property>
</joined-subclass>
</class>
</hibernate-mapping>
因此信用卡和银行帐户都继承自“账单详细信息”。在我的域层中,我有以下声明:
var billingDetail = (from a in unitOfWork.Context.BillingDetail.OfType<CreditCard>()
select a).FirstOrDefault();
上面的“unitOfWork.Context”属性仅允许我访问 ISession 工作单元。当我运行应用程序时,出现以下错误:
BillingDetail = 'unitOfWork.Context.BillingDetail' 引发了类型为 'NHibernate.PropertyAccessException' 的异常 Message =“Invalid Cast(检查属性类型不匹配的映射);BankAccount 设置器”
如果我在“var billingDetail...”语句处放置断点并运行程序来检查此错误,我可以看到上述消息。但是,如果我随后单击“播放”继续执行程序,程序将成功运行并将所有数据输入数据库,而不是出现上述消息。如果不检查断点,程序就会崩溃(正如我所期望的,如果映射实际上存在问题)。
似乎发生的情况是,每次看到“BankAccount”实体都会给出例外,但所有信用卡实体都很好。 “OfType”是否有问题,它没有过滤掉 BankAccount 对象?
I have a simple table-per-subclass inheritance with the following NHibernate mapping
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default- cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="BillingDetail table="BillingDetails">
<id name="Id" type="System.Int32">
<column name="Id" />
<generator class="identity" />
</id>
<property name="DateAdded" type="System.DateTime">
<column name="DateAdded" />
</property>
<many-to-one class="Account name="Account">
<column name="Account_id" />
</many-to-one>
<joined-subclass name="BankAccount table="BillingDetails_BankAccount">
<key>
<column name="Id"/>
</key>
<property name="AccountNumber" type="System.Int64">
<column name="AccountNumber" />
</property>
<property name="SortCode" type="System.Int32">
<column name="SortCode" />
</property>
</joined-subclass>
<joined-subclass name="CreditCard table="BillingDetails_CreditCard">
<key>
<column name="Id" />
</key>
<property name="CardNumber" type="System.Int64">
<column name="CardNumber" />
</property>
<property name="CardType" type="System.String">
<column name="CardType" />
</property>
<property name="ExpiryDate" type="System.DateTime">
<column name="ExpiryDate" />
</property>
</joined-subclass>
</class>
</hibernate-mapping>
So both Credit Card and Bank Account inherit from "Billing Detail". Within my domain layer I have the following statement:
var billingDetail = (from a in unitOfWork.Context.BillingDetail.OfType<CreditCard>()
select a).FirstOrDefault();
The "unitOfWork.Context" property above just gives me access to the ISession unit of work. When I run the application I get the following error:
BillingDetail = 'unitOfWork.Context.BillingDetail' threw an exception of type 'NHibernate.PropertyAccessException'
Message = "Invalid Cast (check your mapping for property type mismatches); setter of BankAccount"
If I put a breakpoint at the "var billingDetail..." statement and run the program to inspect this error I can see the above message. However, if I then click play to continue program execution, instead of bombing out with the above message, the program runs successfully and enters all the data into the database. Without inspecting the breakpount, the program crashes (as I'd expect if there's actually a problem with the mappings).
What appears to be happening is that every time it sees a "BankAccount" entity is gives the exception, but all CreditCard entities are fine. Is there something wrong with the "OfType" that it doesn't filter out the BankAccount objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的答案没什么有趣的。我应该更加注意完整的错误,其中显示:
无效的强制转换(检查属性类型不匹配的映射)
当我执行错误告诉我的操作时,我看到一列映射为 Int32,但应该是 Int64。我不太聪明。
Answer here was nothing interesting. I should have paid more attention to the full error which said:
Invalid Cast (check your mapping for property type mismatches)
When I did what the error told me I saw a column which was mapped as an Int32 but should have been Int64. Not very clever of me.