NHibernate:“无法确定类型”异常

时间:2011-10-05 14:28:29

标签: c# mysql nhibernate

我有一个使用NHibernate的C#.NET 3.5应用程序将对象映射到MySQL数据库。

MySQL表将测试用例与文件服务器相关联。有一个中间表,用于确定给定服务器是否用于测试用例文件或结果。

testcase { TestCaseID }
testcase_servers { TestCaseServerID, ResultServerID, FileServerID, TestCaseID }
file_server { [INT(11)]ServerID, [VARCHAR(45)]Username, [VARCHAR(45)]Password, [VARCHAR(45)]Address }

在C#代码中,我没有中间类。

class TestCase
{
    public virtual int Id { get; set; }

    // other properties ommitted for brevity

    public virtual Server Server { get; set; }
}

class Server
{
    public virtual int Id { get; set; }
    public virtual string Address { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

我尝试使用的映射使用公式将正确的服务器映射到TestCase。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyApplication"
                   namespace="MyProject">

    <class name="Server" table="file_server">
        <id name="Id" column="ServerID" type="int" >
            <generator class="native"/>
        </id>
        <property name="Address"/>
        <property name="Username"/>
        <property name="Password"/>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyApplication"
                   namespace="MyProject">

    <class name="TestCase" table="testcase">
        <id name="Id" column="TestCaseID" type="int" >
            <generator class="native"/>
        </id>
        <property name="Server" type="Server" formula="(SELECT file_server.* FROM testcase_servers, file_server WHERE testcase_servers.ResultServerID = file_server.ServerID AND testcase_servers.TestCaseID = Id)">
        </property>
    </class>
</hibernate-mapping>

但是,我得到一个例外:

Could not determine type for: MyProject, MyApplication, for columns: NHibernate.Mapping.Formula( (SELECT queue_file_server.* FROM queue_tc_servers, queue_file_server WHERE queue_tc_servers.ResultServerID = queue_file_server.ServerID AND queue_tc_servers.TestCaseID = Id) )

如何在不为中间表创建另一个对象的情况下将服务器映射到测试用例?

谢谢, PaulH

修改 我已尝试将Server设为IUserType

class Server : IUserType
{
    public virtual int Id { get; set; }
    public virtual string Address { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    // IUserType Members (some ommitted for brevity)

    public NHibernate.SqlTypes.SqlType[] SqlTypes
    {
        get
        {
            return new SqlType[] {
                new SqlType(DbType.Int32),
                new SqlType(DbType.String),
                new SqlType(DbType.String),
                new SqlType(DbType.String),
            };
        }
    }
}

我现在获得MappingException Property mapping has wrong number of columns: MyProject.TestCase.Server type: Server

0 个答案:

没有答案