Nhibernate 3.1 .Net 4.0 - 从查询列表中获取 Composite-Id
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将复合键命名为:
并将一个属性放入 TabMaster 实体中:
这样您将能够看到实体上的单个键属性。
You should name the composite key:
and put a property in the TabMaster entity:
with this you will be able to see the single key property on the entity.