0

Have Books class.

public class Books
{

    public virtual int Id { get; set; }
    public virtual string Title { get; set; }

    public virtual string Category { get; set; }

    public virtual string ISBN { get; set; }

    public virtual string Description { get; set; }

    public virtual string Image { get; set; }

    public virtual int CategoryId { get; set; }

    public Categories Categories { get; set; }

    public virtual IList<Comments> Comments { get; set; }

    public Books()
    {
        Comments = new List<Comments>();
    }

}

Also Comments Class.

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

    public virtual string CommentText { get; set; }

    public virtual DateTime Date { get; set; }

    public virtual int IdBook { get; set; }

    public Books Books { get; set; }
}

My map code for Books class :

public class BooksMap : ClassMap <Books>
{
    public BooksMap()
    {
        Id(x => x.Id);
        Map(x => x.Title);
        Map(x => x.ISBN);
        Map(x => x.Description);
        Map(x => x.Category);
        Map(x => x.Image);
        Map(x => x.CategoryId);
        //reference to categories
        References(x => x.Categories).Column("Id").Not.Nullable();
        //inverse reference (hasmany comments, rating)
        HasMany(x => x.Comments).Cascade.All().Inverse();
    }
}

Also Comments map >

public class CommentsMap:ClassMap<Comments>
{
    public CommentsMap()
    {
        Id(x => x.Id);
        Map(x => x.CommentText);
        Map(x => x.Date);
        Map(x => x.IdBook);
        References(x => x.Books).Column("Id").Not.Nullable();
    }
}

My question is : am I doing it right? - and how to make queries with this mapings for example criteria language?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
LoverBugs
  • 127
  • 1
  • 2
  • 14

1 Answers1

2

It would be nice to know what is NOT working (instead of asking "is this ok?")

There is some summary, what could be the way to properly map one-to-many and many-to-one:

Minimal and correct way to map one-to-many with NHibernate

Anyhow, all properties must be virtual

public class Books
{
    public virtual int Id { get; set; }
    ...

    public virtual int CategoryId { get; set; }
    // all must be virtual
    //public Categories Categories { get; set; }
    public virtual Categories Categories { get; set; }

    public virtual IList<Comments> Comments { get; set; }

As we can also see, there is CategoryId and Category - doubled mapping of one column (once as Reference once as ValueType). It means, that one of these must be readonly:

public BooksMap()
{
    Id(x => x.Id);
    ...
    // this should be readonly
    // Map(x => x.CategoryId);
    Map(x => x.CategoryId)
       .Not.Update()
       .Not.Insert();
    //reference to categories
    References(x => x.Categories)...

Reference should be mapped to column representing the Categroy_ID not the Id (which is already used for property x.Id)

// this does not seem to be ok
References(x => x.Categories).Column("Id").Not.Nullable();
// we would need different column
References(x => x.Categories)
    .Column("Category_ID")
    .Not.Nullable();

I would also expect, that in the Comments table is some column called "Book_ID". This is column, which we will use for HasMany() mapping.

HasMany(x => x.Comments)
    .KeyColumn("Book_ID")
    .Cascade.AllDeleteOrphan()
    .Inverse();

The same column "Book_ID" must be used on the other side

public CommentsMap()
{
    Id(x => x.Id);
    ...
    References(x => x.Books)
        .Column("Book_ID")
        .Not.Nullable();
}

So, this mapping should be ready to use now. To get some ideas about querying, check the doc with lot of examples:

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • should i map Book_ID first or should i just add in reference to other table when i map the comments ?? This : `References(x => x.Books) .Column("Book_ID") .Not.Nullable();` Or : `Map(x=>x.Book_ID);` and then `References(x => x.Books) .Column("Book_ID") .Not.Nullable();` – LoverBugs Sep 02 '15 at 14:03
  • The order of mapping files is not important. One *(`References`)* belongs to `Comment` mapping, while the second *(`HasMany`)* belongs to `Book` mapping. Both are needed, and NHibernate will process them in proper order for us... hope it helps a bit – Radim Köhler Sep 02 '15 at 14:59