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`)
) 

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.

1 Answer 1

2

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");
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hmm ok, then how do I map that list to my schema? Sorry still not fully understanding how nhibernate works =\
I'm playing with Fluent Nhibernate and it is helping me understand, but it looks like a Director class would need an IList<Movie> to work. Is there anyway to avoid this?
@user623879: It doesn't need to, if you don't have a plan yet to iterate to the movies directed by a director, you can remove IList<Movie> under the Director class. You can try this example: ienablemuch.com/2011/07/… Try to remove the IList<Movie> of Genre class, the code will still work. It's trivial to setup IList on both sides of the class relation though, it's ok to just put them all :-)
+1. Name of the bag missing: <bag name=0"Directors" table="m_directs">. You can also remove the columns which are identical to the name.
@Stefan Steinegger thx fixed. I'm not used to writing xml, mostly writing fluent mappings ;)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.