Parameterized Queries in HQL

NHibernate supports parameterized queries but unless you go looking in the Java documentation it’s not obvious how to do it.  Turns out it’s really simple:

IList results = _session.Find(“from MyAssembly.MyClass as MyClass where MyClass.Foo = ?”,
   
tagText, TypeFactory.GetStringType(64));

You can pass in arrays of objects and types for the last two parameters if you have more than one parameter in your HQL.

One Response to “Parameterized Queries in HQL”

  1. Ayende Rahien Says:

    You can also use named parameters (which I find very useful) using:

    “from MyAssembly.MyClass as MyClass where MyClass.Foo = :fooVal”

Leave a Reply

Powered by WP Hashcash