使用NHibernate从外键获取MySQL表中的数据

时间:2018-03-04 20:56:37

标签: mysql nhibernate

我有一个带有树表的MySQL数据库:Author,PublishingHouse和Book。最后一个有两个外键 - AuthorId和PublishingId。 我为每个表创建了类,为NHibernate创建了一个映射文件。 下一步是从Book表中获取数据并将其传递给我的MVC项目视图。 我无法做到这一点,因为在我的BookController中我得到了这样的异常: List

这是我现在的代码:

模型

NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches);

NHibernate制图

public class Author
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

public class PublishingHouse
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string City { get; set; }

    }

public class Book
    {
        public virtual int Id { get; set; }
        public virtual string UDK { get; set; }
        public virtual string BBK { get; set; }

        public virtual string AuthorsSign { get; set; }
        public virtual string ISBNrus { get; set; }
        public virtual string ISBNeng { get; set; }

        public virtual Author Author { get; set; }
        public virtual string Name { get; set; }
        public virtual PublishingHouse PublishingHouse { get; set; }

        public virtual int Year { get; set; }
        public virtual int Pages { get; set; }
        public virtual int Circulation { get; set; }

        public virtual bool IsRead { get; set; }
        public virtual int Rating { get; set; }
        public virtual string Annotation { get; set; }
    }

BookController 我得到一个例外:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="InFolio_HomeLib" namespace="InFolio_HomeLib.Models">
  <class name="Author" table="Author" dynamic-update="true" >
    <cache usage="read-write"/>
    <id name="Id" column="Id" type="int">
      <generator class="native" />
    </id>
    <property name="FirstName" />
    <property name="LastName" />
  </class>

  <class name="PublishingHouse" table="PublishingHouse" dynamic-update="true" >
    <cache usage="read-write"/>
    <id name="Id" column="Id" type="int">
      <generator class="native" />
    </id>
    <property name="Name" />
    <property name="City" />
  </class>

    <class name="Book" table="Book" dynamic-update="true" >
    <cache usage="read-write"/>
    <id name="Id" column="Id" type="int">
      <generator class="native" />
    </id>
    <property name="UDK" />
    <property name="BBK" />
    <property name="AuthorsSign" />
    <property name="ISBNrus" />
    <property name="ISBNeng" />   

    <set name="Author" table="Author">
      <key column ="Id" />
      <one-to-many class="Author"/>
    </set>    

    <property name="Name" /> 

    <set name="PublishingHouse" table="PublishingHouse">
      <key column ="Id" />
      <one-to-many class="PublishingHouse"/>
    </set>    

    <property name="Year" />
    <property name="Pages" />
    <property name="Circulation" />
    <property name="IsRead" />
    <property name="Rating" />
    <property name="Annotation" />
  </class>
</hibernate-mapping>

我可以轻松地从Author和Publishing表中获取数据,它们非常简单,但Book表对我来说并不那么明显。我在绘图时错了吗?或者在其他地方?

1 个答案:

答案 0 :(得分:0)

Book类

<class name="Book" table="Book" dynamic-update="true" >

有一位作者和一家出版社。我们需要多对一的映射

所以,而不是这个:

<set name="Author" table="Author">
  <key column ="Id" />
  <one-to-many class="Author"/>
</set>    

<set name="PublishingHouse" table="PublishingHouse">
  <key column ="Id" />
  <one-to-many class="PublishingHouse"/>
</set>  

我们需要:

<many-to-one name="Author" column="Author_ID" />
<many-to-one name="PublishingHouse" column="PublishingHouse_ID" />

期望Book表具有引用列Author_ID和PublishingHouse_ID

文档:

  

5.1.11. many-to-one

     

使用声明与另一个持久类的普通关联   多对一的元素。关系模型是多对一的   协会。 (它实际上只是一个对象参考。)...

另外,不要错过这个: