The Inquisitive Coder – Davy Brion's Blog

Trying to walk that thin line between intelligence and ignorance

One Of My Favorite NHibernate 2.1 Features

Posted by Davy Brion on July 30th, 2009

Check out the following piece of code:


            Session.CreateQuery(
                @"delete from DocumentTypeAssignment
                  where Id.DmsDocument in (from DmsDocument where Id = :documentId) and
                        Id.DocumentType.Id not in (:newDocumentTypeIds)")
                .SetInt64("documentId", dmsDocumentId)
                .SetParameterList("newDocumentTypeIds", newDocumentTypesToAssign.ToList(), NHibernateUtil.Int64)
                .ExecuteUpdate();

(pay no attention to Id.DmsDocument or Id.DocumentType… it’s a composite key for a legacy table)

Which results in this SQL statement:


delete
    from
        DocumentManagement.DocumentTypeAssignment
    where
        (
            DmsDocumentID in (
                select
                    dmsdocumen1_.ID
                from
                    DocumentManagement.DmsDocument dmsdocumen1_
                where
                    dmsdocumen1_.ID=@p0
            )
        )
        and (
            DocumentTypeID not in  (
                @p1 , @p2
            )
        );
    @p0 = 1634, @p1 = 2313, @p2 = 2310

4 Responses to “One Of My Favorite NHibernate 2.1 Features”

  1. den Ben Says:

    pretty nice but I’d put that SQL statement in a named query in the corresponding mapping file

    just to isolate all the database version/brand dependent stuff inside those mapping files

  2. Davy Brion Says:

    There is no SQL to write :)

    though you could put the db-independent HQL in a named query of course

  3. den Ben Says:

    ah… my bad :) ‘t is HQL not SQL; sweet

    yeah, i’d still put that in a named query though

  4. Arjan’s World » LINKBLOG for August 1, 2009 Says:

    [...] One Of My Favorite NHibernate 2.1 Features – Davy Brion [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>