One Of My Favorite NHibernate 2.1 Features

4 commentsWritten on July 30th, 2009 by
Categories: NHibernate

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

  • http://benpittoors.wordpress.com den Ben

    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

  • http://davybrion.com Davy Brion

    There is no SQL to write :)

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

  • http://benpittoors.wordpress.com den Ben

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

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

  • Pingback: Arjan’s World » LINKBLOG for August 1, 2009