NHibernate 映射问题

发布于 2024-11-27 13:50:55 字数 936 浏览 2 评论 0原文

CREATE TABLE IF NOT EXISTS `movie` (
  `m_ID` varchar(9) NOT NULL
  `ReleaseDate` date 
  `Title` varchar(255) NOT NULL 
  PRIMARY KEY (`m_ID`),
  KEY `m_ID` (`ReleaseDate`,`Title`,)
)
CREATE TABLE IF NOT EXISTS `m_director` (
  `dirID` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `dirName` varchar(40) NOT NULL,
  PRIMARY KEY (`dirID`),
  UNIQUE KEY `dirName` (`dirName`)
) 
CREATE TABLE IF NOT EXISTS `m_directs` (
  `m_ID` char(9) NOT NULL
  `dirID` int(11) unsigned NOT NULL,
  UNIQUE KEY `m_ID_2` (`m_ID`,`dirID`),
  KEY `m_ID` (`m_ID`),
  KEY `dirID` (`dirID`)
) 

另请注意,m_ID 设置为 movie 表的外键,dirID 设置为 m_director 的外键。

我不明白如何通过映射 xml 将其映射到这样的对象。

public class Movie
{
public string MovieTitle{get;set;}
public IList<string> Directors;
public DateTime ReleaseDate;
}

我可以看到,如果 movie 和 m_director (例如 m_directs)之间没有中间表,那么使用列表标记会相当容易,但是对于像我这样的模式,我不明白这是如何完成的。

CREATE TABLE IF NOT EXISTS `movie` (
  `m_ID` varchar(9) NOT NULL
  `ReleaseDate` date 
  `Title` varchar(255) NOT NULL 
  PRIMARY KEY (`m_ID`),
  KEY `m_ID` (`ReleaseDate`,`Title`,)
)
CREATE TABLE IF NOT EXISTS `m_director` (
  `dirID` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `dirName` varchar(40) NOT NULL,
  PRIMARY KEY (`dirID`),
  UNIQUE KEY `dirName` (`dirName`)
) 
CREATE TABLE IF NOT EXISTS `m_directs` (
  `m_ID` char(9) NOT NULL
  `dirID` int(11) unsigned NOT NULL,
  UNIQUE KEY `m_ID_2` (`m_ID`,`dirID`),
  KEY `m_ID` (`m_ID`),
  KEY `dirID` (`dirID`)
) 

Also note that m_ID is set as a foreign key to movie table and dirID is set as a foreign key to m_director.

I do not understand how this would get mapped to an object like this one via mapping xmls.

public class Movie
{
public string MovieTitle{get;set;}
public IList<string> Directors;
public DateTime ReleaseDate;
}

I can see it would be fairly easy with a list tag if there was not an intermediary table between movie and m_director(eg m_directs), but for a schema like I have, I do not understand how this is done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2024-12-04 13:50:55

AFAIK public IList;导演; 不能是多个,因为字符串没有对象标识,因此无法从不同的电影链接。因此你不能用 NHibernate 来映射它。最好的方法是

public class Movie
{
    public virtual string Title { get; set; }
    public virtual ICollection<Director> Directors { get; set; }
    public virtual DateTime ReleaseDate { get; set; }

    public virtual IEnumerable<string> Directornames
    { get { return Directors.Select(dir => dir.Name); } }
}

public class Director
{
    public virtual int Id {get; protected set;}
    public virtual string Name {get; set;}
}

  <class name="Movie" table="movie">
    <id name="Id" column="m_ID"/>
    <property name="ReleaseDate"/>
    <property name="Title"/>
    <bag name="Directors" table="m_directs">
      <key column="m_ID" not-null="true"/>
      <many-to-many class="Director" column="dirID"/>
    </bag>
  </class>

  <class name="Director" table="m_director">
    <id name="Id" column="dirID"/>
    <property name="Name" column="dirName"/>
  </class>

向董事提供更多信息,例如出生日期等。

编辑:更正了 xml 映射的拼写错误和缩进(因此未显示)并添加了流畅的映射

class MovieMap : ClassMap<Movie>
{
    public MovieMap()
    {
        Table("movie");

        Id(m => m.Id, "m_Id").GeneratedBy.Identity();

        Map(m => m.Title);
        Map(m => m.ReleaseDate);

        HasManyToMany(m => m.Directors)
            .Table("m_directs")
            .ParentKeyColumn("m_ID")
            .ChildKeyColumn("dirID")
            .AsBag()
            .Cascade.All();
    }
}

class DirectorMap : ClassMap<Director>
{
    public DirectorMap()
    {
        Table("m_director");

        Id(d => d.Id, "dirID").GeneratedBy.Identity();

        Map(d => d.Name, "dirName");
    }
}

AFAIK public IList<string> Directors; cant be a manytomany because string has no object identity so it cant be linked from different Movies. Therefor you cant map it with NHibernate as manytomany. best way would be to

public class Movie
{
    public virtual string Title { get; set; }
    public virtual ICollection<Director> Directors { get; set; }
    public virtual DateTime ReleaseDate { get; set; }

    public virtual IEnumerable<string> Directornames
    { get { return Directors.Select(dir => dir.Name); } }
}

public class Director
{
    public virtual int Id {get; protected set;}
    public virtual string Name {get; set;}
}

  <class name="Movie" table="movie">
    <id name="Id" column="m_ID"/>
    <property name="ReleaseDate"/>
    <property name="Title"/>
    <bag name="Directors" table="m_directs">
      <key column="m_ID" not-null="true"/>
      <many-to-many class="Director" column="dirID"/>
    </bag>
  </class>

  <class name="Director" table="m_director">
    <id name="Id" column="dirID"/>
    <property name="Name" column="dirName"/>
  </class>

then it would be also possible to give directors more information like Birthdate and so on.

Edited: corrected typos and indentation of xml mapping (hence not shown) and added fluent mapping

class MovieMap : ClassMap<Movie>
{
    public MovieMap()
    {
        Table("movie");

        Id(m => m.Id, "m_Id").GeneratedBy.Identity();

        Map(m => m.Title);
        Map(m => m.ReleaseDate);

        HasManyToMany(m => m.Directors)
            .Table("m_directs")
            .ParentKeyColumn("m_ID")
            .ChildKeyColumn("dirID")
            .AsBag()
            .Cascade.All();
    }
}

class DirectorMap : ClassMap<Director>
{
    public DirectorMap()
    {
        Table("m_director");

        Id(d => d.Id, "dirID").GeneratedBy.Identity();

        Map(d => d.Name, "dirName");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文