我有一个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。
有什么问题?
我认为这与复合ID规则(?)
有关感谢您的回复!
答案 0 :(得分:0)
您应该命名复合键:
<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; }
}
通过这个,您将能够看到实体上的单个键属性。