开发者

Nhibernate 3.1 .Net 4.0 - Get Composite-Id from List of Query

开发者 https://www.devze.com 2023-03-24 01:00 出处:网络
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\" ?>

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开发者_StackOverflow 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消