Nhibernate 3.1 .Net 4.0 - 从查询列表中获取 Composite-Id

发布于 2024-11-26 18:34:47 字数 3865 浏览 2 评论 0原文

我有一个 MS SQL 数据库,其中有一个具有复合 id 的表。

这是我的 xml 配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="SimpleMapping.Domain"
namespace="SimpleMapping.Domain">
  <class name="TabMaster" table="TabMaster">
    <composite-id class="TabMasterCompositeKey">
      <key-property column="Configuration" name="Configuration" type="AnsiString" />
      <key-property column="ResolutionType" name="ResolutionType" type="int" />
    </composite-id>
    <property name="Description" column="Description" type="AnsiString" />
    <property name="Title" column="Title" type="AnsiString" />
    <property name="IdResolutionFileDes" column="idResolutionFileDes" type="AnsiString" />
    <property name="WorkflowType" column="WorkflowType" type="AnsiString" />
    <property name="ViewAllStep" column="ViewAllStep" type="AnsiString" />
    <property name="ManagedData" column="ManagedData" type="AnsiString" />
  </class>
</hibernate-mapping>

我已经创建了映射对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleMapping.Domain
{
    public class TabMaster
    {
        public virtual string Configuration { get; set; }
        public virtual int ResolutionType { get; set; }
        public virtual string Description { get; set; }
        public virtual string Title { get; set; }
        public virtual string IdResolutionFileDes { get; set; }
        public virtual string WorkflowType { get; set; }
        public virtual string ViewAllStep { get; set; }
        public virtual string ManagedData { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleMapping.Domain
{
    class TabMasterCompositeKey
    {
        public virtual string Configuration { get; set; }
        public virtual int ResolutionType { get; set; }
        public override bool Equals(object obj)
        {
            TabMasterCompositeKey compareTo = (TabMasterCompositeKey)obj;
            return (this.Configuration == compareTo.Configuration) && (this.ResolutionType == compareTo.ResolutionType);
        }
        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }
        public override string ToString()
        {
            return Configuration.ToString() + "/" + ResolutionType.ToString();
        }
    }
}

我的 Main() 中,我尝试列出表中的元素:

namespace SimpleMapping.Console
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NHibernate;
    using NHibernate.Cfg;
    using SimpleMapping.Domain;

    class Program
    {
        static void Main(string[] args)
        {
            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();

            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {

                    IQuery query = session.CreateQuery("from TabMaster");

                    foreach (TabMaster tm in query.List<TabMaster>())
                        System.Console.WriteLine(string.Format("ID: {0}\nConfiguration: {1}\nManagedData: {2}\n", tm.Configuration, tm.Description, tm.ManagedData));

                    tx.Commit();
                    session.Close();
                }
            }

            System.Console.ReadKey();
        }
    }
}

我可以看到不是键的参数。 因此,我可以看到 Description 和 ManagedData,但看不到 tm.Configuration :堆栈中的每条记录都设置为 Null。

有什么问题吗?

我认为这与composite-id规则有关(?)

谢谢您的回复!

I have a MS SQL db with a table that has a composite id.

This is my xml configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="SimpleMapping.Domain"
namespace="SimpleMapping.Domain">
  <class name="TabMaster" table="TabMaster">
    <composite-id class="TabMasterCompositeKey">
      <key-property column="Configuration" name="Configuration" type="AnsiString" />
      <key-property column="ResolutionType" name="ResolutionType" type="int" />
    </composite-id>
    <property name="Description" column="Description" type="AnsiString" />
    <property name="Title" column="Title" type="AnsiString" />
    <property name="IdResolutionFileDes" column="idResolutionFileDes" type="AnsiString" />
    <property name="WorkflowType" column="WorkflowType" type="AnsiString" />
    <property name="ViewAllStep" column="ViewAllStep" type="AnsiString" />
    <property name="ManagedData" column="ManagedData" type="AnsiString" />
  </class>
</hibernate-mapping>

I have created my mapping objects:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleMapping.Domain
{
    public class TabMaster
    {
        public virtual string Configuration { get; set; }
        public virtual int ResolutionType { get; set; }
        public virtual string Description { get; set; }
        public virtual string Title { get; set; }
        public virtual string IdResolutionFileDes { get; set; }
        public virtual string WorkflowType { get; set; }
        public virtual string ViewAllStep { get; set; }
        public virtual string ManagedData { get; set; }
    }
}

and

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleMapping.Domain
{
    class TabMasterCompositeKey
    {
        public virtual string Configuration { get; set; }
        public virtual int ResolutionType { get; set; }
        public override bool Equals(object obj)
        {
            TabMasterCompositeKey compareTo = (TabMasterCompositeKey)obj;
            return (this.Configuration == compareTo.Configuration) && (this.ResolutionType == compareTo.ResolutionType);
        }
        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }
        public override string ToString()
        {
            return Configuration.ToString() + "/" + ResolutionType.ToString();
        }
    }
}

In my Main() I try to list the elements in the table doing:

namespace SimpleMapping.Console
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NHibernate;
    using NHibernate.Cfg;
    using SimpleMapping.Domain;

    class Program
    {
        static void Main(string[] args)
        {
            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();

            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {

                    IQuery query = session.CreateQuery("from TabMaster");

                    foreach (TabMaster tm in query.List<TabMaster>())
                        System.Console.WriteLine(string.Format("ID: {0}\nConfiguration: {1}\nManagedData: {2}\n", tm.Configuration, tm.Description, tm.ManagedData));

                    tx.Commit();
                    session.Close();
                }
            }

            System.Console.ReadKey();
        }
    }
}

and I can see the parameters that are not a key.
So I can see Description and ManagedData but I can't see tm.Configuration : in the stack is set to Null for every record.

What's the problem?

I think this is related to the composite-id rule (?)

Thank you for your reply!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

北斗星光 2024-12-03 18:34:47

您应该将复合键命名为:

<composite-id class="TabMasterCompositeKey" name="TheKey">
      <key-property column="Configuration" name="Configuration" type="AnsiString" />
      <key-property column="ResolutionType" name="ResolutionType" type="int" />
    </composite-id>

并将一个属性放入 TabMaster 实体中:

public class TabMaster
    {
        public virtual TabMasterCompositeKey TheKey { get; set; }

        public virtual string Description { get; set; }
        public virtual string Title { get; set; }
        public virtual string IdResolutionFileDes { get; set; }
        public virtual string WorkflowType { get; set; }
        public virtual string ViewAllStep { get; set; }
        public virtual string ManagedData { get; set; }
    }

这样您将能够看到实体上的单个键属性。

You should name the composite key:

<composite-id class="TabMasterCompositeKey" name="TheKey">
      <key-property column="Configuration" name="Configuration" type="AnsiString" />
      <key-property column="ResolutionType" name="ResolutionType" type="int" />
    </composite-id>

and put a property in the TabMaster entity:

public class TabMaster
    {
        public virtual TabMasterCompositeKey TheKey { get; set; }

        public virtual string Description { get; set; }
        public virtual string Title { get; set; }
        public virtual string IdResolutionFileDes { get; set; }
        public virtual string WorkflowType { get; set; }
        public virtual string ViewAllStep { get; set; }
        public virtual string ManagedData { get; set; }
    }

with this you will be able to see the single key property on the entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文