开发者

Querying a many-to-many collection or how to include a many-to-many table in a criteria query?

I am quite new to the world of NHibe开发者_如何学Crnate and I can't seem to get this to work with the use of a criteria query: query a many-to-many relationship or query a collection (set/bag) on an entity. I've searched the internet and checked all the NHibernate books we have, but I can't find a specific answer to my "challenge".

I have made a simplified example of the problem I'm trying to solve. I have a table with books, a table with categories and a many-to-many table with the categories per book. Here are some of the technicalities:

data structure:

create table tableBook
(
    BkId       integer   not null default autoincrement,    
    BkTitle    char(40)  not null,
    BkWriter   char(40)  not null,
    primary key (BkId)
);

create table tableCategory
(
    CatId       integer   not null default autoincrement,    
    CatCode     char(3)   not null,
    CatDesc     char(40),
    primary key (CatId)
);

create table tableCategoriesPerBook
(
    CpbId        integer         not null default autoincrement,
    CpbBkId      integer         not null, /*foreign key to tableBook*/
    CpbCatId     integer         not null, /*foreign key to tableCategory*/
    primary key (CpbId)
);

alter table tableCategoriesPerBook add foreign key FK_CpbBkId (CpbBkId) references tableBook (BkId) on update Restrict on delete Cascade;
alter table tableCategoriesPerBook add foreign key FK_CpbCatId (CpbCatId) references tableCategory (CatId) on update Restrict on delete Cascade;

create unique index idx_CpbCatId_CpbBkId on tableCategoriesPerBook (CpbCatId, CpbBkId);

C# classes:

public class BookEntity
{
    public virtual Int32 BookId { get; set; }
    public virtual string BookTitle { get; set; }
    public virtual string BookWriter { get; set; }

    private readonly IEnumerable<CategoryEntity> _categories = new ObservableCollection<CategoryEntity>();
    public virtual IEnumerable<CategoryEntity> Categories
    {
        get { return _categories; }            
    }
}

public class CategoryEntity
{
    public virtual Int32 CategoryId { get; set; }
    public virtual string CategoryCode { get; set; }
    public virtual string CategoryDesc { get; set; }
}

NHibernate mappings:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Domain" assembly="Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Domain.BookEntity" table="tableBook">
    <id name="BookId" column="BkId" type="Int32">
      <generator class="native" />
    </id>
    <property name="BookTitle" column="BkTitle" type="string" length="40"/>
    <property name="BookWriter" column="BkWriter" type="string" length="40"/>    
    <idbag name="_categories" access="field" table="tableCategoriesPerBook">        
        <collection-id type="Int32" column="CpbId">
          <generator class="native"/>
        </collection-id>        
        <key column="CpbBkId" property-ref="BkId"/>        
        <many-to-many column="CpbCatId" class="Domain.CategoryEntity, Domain" />
    </idbag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Domain" assembly="Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Domain.CategoryEntity" table="tableCategory">    
    <id name="CategoryId" column="CatId" type="Int32">
      <generator class="native" />
    </id>
    <property name="CategoryCode" column="CatCode" type="string" length="3" />
    <property name="CategoryDesc" column="CatDesc" type="string" length="40" />    
  </class>
</hibernate-mapping>

My question: is it possible to query (using ICriteria and/or detached criterias) the database in such a way that I get the books which is in one of the categories I specify (for instance: in catA or catB, could be "and" as well)? I want to optimize this in the query, not in C# (as I need to read all books from the database before I can filter the objects based on their collection of tags). If I'd write the SQL by hand, I would produce something like this:

SELECT * FROM tableBook                                                                                                                           
WHERE EXISTS 
    (
     SELECT 1 
     FROM   tableCategoriesPerBook 
            INNER JOIN tableCategory on (CpbCatId = CatId and CpbBkId = BkId) 
     WHERE  CatCode in ('001', '002')
    )

Since I don't have an entity for tableCategoriesPerBook, I don't see a way to get to this table with a criteria query. And I'd rather not add some handwritten piece of SQL expressions using:

criteria.Add(Expression.Sql("exists(.....)");

One last important factor: I am using a brownfield database, so I can't change the structure! this is what I'll have to work with database-wise.


This is pretty straight forward. You can use a detached criteria.

DetachedCriteria bookCategoryCriteria = DetachedCriteria.For<BookEntity>("bookCat");
bookCategoryCriteria
    .CreateAlias("Categories", "cat", JointType.LeftOuterJoin)
    .Add(Restrictions.In("cat.CategoryCode", categories)
    .Add(Restrictions.Eq("bookCat.BookId", "book.BookId")
    .SetProjection(Projections.Id());

Session.CreateCriteria<BookEntity>("book")
   .Add(Subqueries.Exists(bookCategoryCriteria));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜