NHibernate 映射自连接表?
这可能很简单,但我想我错过了一些东西。我有一个自连接表 Units
,每个单元都有一个主单元:
这样我就可以查询如下内容:
表 units
已映射到以下类:
public class Units
{
public virtual int Unit_Id { get; private set; }
public virtual string Unit { get; set; }
public virtual decimal Unit_Value { get; set; }
public virtual Units Main_Unit { get; set; }
}
使用 .hbm映射文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NewA.Domain" namespace="NewA.Domain">
<class name="NewA.Domain.Entities.Units,NewA.Domain" table="Units">
<id name="Unit_Id" column="Unit_Id" type="Int32" length="4" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="Unit" column="Unit" type="string" length="50" not-null="true"/>
<one-to-one name="Main_Unit" class="NewA.Domain.Entities.Units,NewA.Domain"/>
</class>
</hibernate-mapping>
当使用以下代码测试这些实体时:
Units unit = _UnitsRepository.GetById(2);
string parent_unitname = unit.Main_Unit.Unit;
Assert.AreEqual("pack",parent_unitname);
我得到以下例外:
预期值相等。 期望值:“包” 实际值:“公斤”
问题是 Unit
实体的 Main_Unit
属性正在引用自身,所以我在这里缺少什么??以及如何编写类似递归 SQL CTE 的内容来应用排名等等,因为我对其他具有更复杂查询的更复杂自连接表也有同样的问题。
It might be very simple but I think I am missing something. I have a self joined table Units
, every unit has a main unit:
So that I could query for something like this:
The table units
is mapped to the following class:
public class Units
{
public virtual int Unit_Id { get; private set; }
public virtual string Unit { get; set; }
public virtual decimal Unit_Value { get; set; }
public virtual Units Main_Unit { get; set; }
}
With .hbm mapping file as follows:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NewA.Domain" namespace="NewA.Domain">
<class name="NewA.Domain.Entities.Units,NewA.Domain" table="Units">
<id name="Unit_Id" column="Unit_Id" type="Int32" length="4" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="Unit" column="Unit" type="string" length="50" not-null="true"/>
<one-to-one name="Main_Unit" class="NewA.Domain.Entities.Units,NewA.Domain"/>
</class>
</hibernate-mapping>
When testing these entity with the following code:
Units unit = _UnitsRepository.GetById(2);
string parent_unitname = unit.Main_Unit.Unit;
Assert.AreEqual("pack",parent_unitname);
I got the following excpetion:
Expected values to be equal. Expected Value : "pack" Actual Value : "kg"
The problem is that the Main_Unit
property of the Unit
entity is referencing itself, so what I am missing here??, and how can I write something like recursive SQL CTE to apply ranks and so on, because I have the same problem with other more complex self joined tables with more complex queries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要多对一映射而不是一对一映射。
尝试这个配置:
我建议你尝试Fluent NHibernate - 它可以为你动态生成映射。
这是我使用的配置:
You need many-to-one mapping instead one-to-one.
Try this configuration:
I suggest you to try Fluent NHibernate - it can dynamically generate mappings for you.
Here's the configuration I used: