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
July 30th, 2009 at 11:46 am
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
July 30th, 2009 at 11:47 am
There is no SQL to write
though you could put the db-independent HQL in a named query of course
July 30th, 2009 at 3:24 pm
ah… my bad
‘t is HQL not SQL; sweet
yeah, i’d still put that in a named query though
August 1st, 2009 at 11:09 pm
[...] One Of My Favorite NHibernate 2.1 Features – Davy Brion [...]