NHibernate Pitfalls: SQL Queries and Parameter Prefix

This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.

When you execute an SQL query with NHibernate with named parameters, you would perhaps expect them to be prefixed with a symbol specific to the current DB you are accessing, for example, @ for SQL Server and : for Oracle; that is not the case. NHibernate requires that you always use it’s DB-neutral parameter prefix, :, the same that you use for HQL queries:

   1: //for SQL queries
   2: session.CreateSQLQuery("SELECT * FROM [Order] WHERE Id = :id").SetParameter("id", 1).List();
   3: //for HQL queries
   4: session.CreateQuery("from Order where id = :id").SetParameter("id", 1).List();


1 Comment

  • this is not a pitfall at all, rather this is desired feature because you can write database independent queries that work on any database. you will see its merit when you switch dbs (or install your software on a client's existing db platform that is different from your development db)

Comments have been disabled for this content.