NHibernate 映射自连接表?

发布于 2024-11-16 04:24:56 字数 1601 浏览 3 评论 0原文

这可能很简单,但我想我错过了一些东西。我有一个自连接表 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:

enter image description here

So that I could query for something like this:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2024-11-23 04:24:56

您需要多对一映射而不是一对一映射。
尝试这个配置:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="NewA.Domain.Entities.Units.Units, NewA.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Units`">
    <id name="Unit_Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit_Id" />
      <generator class="native" />
    </id>
    <property name="Unit" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit" />
    </property>
    <property name="Unit_Value" type="System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit_Value" />
    </property>
    <many-to-one class="NewA.Domain.Entities.Units.Units, NewA.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Main_Unit">
      <column name="Main_Unit_id" />
    </many-to-one>
  </class>
</hibernate-mapping>

我建议你尝试Fluent NHibernate - 它可以为你动态生成映射。
这是我使用的配置:

var fluent = Fluently.Configure()
    .Mappings(c => c.AutoMappings.Add(AutoMap.AssemblyOf<Units>()
        .Override<Units>(u => u.Id(uu => uu.Unit_Id).GeneratedBy.Native())))
    .Database(() => SQLiteConfiguration.Standard.UsingFile("test.sqlite3"));

var configuration = fluent.BuildConfiguration();

// Generate database schema
new SchemaExport(configuration).Create(false, true);

var sessionFactory = configuration.BuildSessionFactory();
// Now just open session and do whatever you need

You need many-to-one mapping instead one-to-one.
Try this configuration:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="NewA.Domain.Entities.Units.Units, NewA.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Units`">
    <id name="Unit_Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit_Id" />
      <generator class="native" />
    </id>
    <property name="Unit" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit" />
    </property>
    <property name="Unit_Value" type="System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit_Value" />
    </property>
    <many-to-one class="NewA.Domain.Entities.Units.Units, NewA.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Main_Unit">
      <column name="Main_Unit_id" />
    </many-to-one>
  </class>
</hibernate-mapping>

I suggest you to try Fluent NHibernate - it can dynamically generate mappings for you.
Here's the configuration I used:

var fluent = Fluently.Configure()
    .Mappings(c => c.AutoMappings.Add(AutoMap.AssemblyOf<Units>()
        .Override<Units>(u => u.Id(uu => uu.Unit_Id).GeneratedBy.Native())))
    .Database(() => SQLiteConfiguration.Standard.UsingFile("test.sqlite3"));

var configuration = fluent.BuildConfiguration();

// Generate database schema
new SchemaExport(configuration).Create(false, true);

var sessionFactory = configuration.BuildSessionFactory();
// Now just open session and do whatever you need
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文