0

Have two tables : Rating and Books. Rating table has foreign key to the Books table.

These tables are mapped this way :

Books :

 HasMany(x => x.RatingList).Cascade.All().Inverse();      

and the Rating table :

 References(x => x.Books).Column("BookId").Cascade.All();

Have this :

var bks = session.CreateCriteria("Books", "b");

using this restriction for selecting books

 bks.Add(Restrictions.Eq("CategoryId",id));

here is the problem, how to join Rating table ??

                    bks.CreateAlias("b.Rating", "c");
                    bks.List();

                    return PartialView("/Views/Home/_Books.cshtml", bks);

The final result i need is to select all Books but also Rating for them. In the Rating table has many ratings for one book. Book rating should be given as average of ratings.

Some help ?

LoverBugs
  • 127
  • 1
  • 2
  • 14

1 Answers1

0

A criteria lets to retrieve a list of a specific class, in your case List<Book>. So, you are asking hibernate to retrieve a book list, not a list of books and ratings.

Of course, you can access ratings for every book into the resulting list. If it doesn't work, maybe a LazyInitialitationException happens. In this case you will have to apply OSIVF, extends session lifetime, or whatever.

Criteria lets you join entities to filter query results. If you create an alias for ratings, it is because you want to filter results with an ratings attributes, but it won't include ratings into the resulting list.

malaguna
  • 4,183
  • 1
  • 17
  • 33