Feeds:
Posts
Comments

Posts Tagged ‘nhibernate’

Howdy ladies and gents out there,

Here we go with another NHibernate common problem solved for you. Recently the question arose whether it is possible or not to execute a query with NHibernate that checks whether a property lies in a given range of values or not. Not much of a problem, we would say, since HQL can do it just directly using its SQL-like syntax. For example, if we recall our example DevJour1 from part 1 of the NHibernate noob series, we could define a query in the Book.hbm.xml file:

   1: <query name="GetAllBooksWithinRange">

   2:   <![CDATA[

   3:     select b from Book b where b.Title in ('It', 'Salem's Lot',                                                  'Langoliers');

   4:   ]]>

   5: </query>

Snippet 1: Excerpt from the Book.hbm.xml file: HQL query returning all books whose title lies within a certain range of values.

Problem solved? Not quite. Another requirement to this query was to provide custom sorting w/ a custom field and a custom sort direction. Sounds familiar, doesn’t it? Remember our first NHibernate troubles post: Custom Sorting. Simply put, using HQL, this is not possible. So we had to use Criteria queries. But how do we implement the range value check for a given property using Criteria queries? It’s simple, really:

   1: public static List<Book> GetAllBooksWithinRange()

   2: {

   3:     String[] titles = { "It", "Salem's Lot", "Langoliers" };

   4:     ISession session = Program.OpenSession();

   5:     List<Book> books = session.CreateCriteria(typeof(Book)).

   6:         Add(Restrictions.In("Title", titles)).

   7:         List<Book>().ToList<Book>();

   8:     return books;

   9: }

Snippet 2: Excerpt from the Book class.

BookGetAllBooksWithinRange()explained (line by line):

3: Definition of our range of values

4: Opening a session, recall from Program class of the Custom Sorting article.

5: Creating criteria for type Book .

6: Crucial part: Adding Restrictions.In (NHibernate.Criterion), passing the property that needs to be checked (Title) and the list of range values.

7-8: Calling the List() method(s) and returning the retrieved list

Important: This works also, when comparing a property of a property with a range of values. Example: Assuming, Book contains a class Author , which in turn contains a property Surname. Then we could formulate such a comparison statement like follows:

.Add(Restrictions.In(“Author.Surname”, names)).


Under the assumption that names is an array containing strings.

That’s it! I’m sure there are more problems to be solved!

See you later!

Best regards,

Martin

Read Full Post »

Hello all,

Want to make your life developing with nhibernate easier? Then, especially if you’re just getting started, you might want to have some IntelliSense support to ease the creation/modification of the .hbm.xml mapping files.

It’s quite simple:

1. Find the XML schema directory of your VS2010 Express (Beta 2, at this time) installation
Tools->Options will point you to the right place. Do not forget to select the Show all settings checkbox at the left bottom of the window:

clip_image002

2. Then, go to the folder you extracted the nhibernate binaries into. Go into the subfolder Required_Bins and copy the two .xsd files nhibernate-configuration.xsd and nhibernate-mapping.xsd

clip_image004

3. Copy them into the XMLSchemas folder you found in step 1

That’s all there is to it!

Without restarting VS2010 Express, the IDE’s IntelliSense will support you from now on with the mapping file’s tags and attributes (show available tags/attributes, help w/ code completion etc.)

Especially in the beginning of your O/R mapping career this will save you a great deal of time and nerves.

Enjoy and check back soon, for there is more to come!

Martin

Read Full Post »

Howdy,

This is my first post here, and I’d like to go right into medias res. The motivation is clear: I am a developer dealing with MS VS & nhibernate in order to map DB schemas. It is not a long time, since I am working on the topic, so I encountered a few troubles that can be shared, I guess. I carried the tests out using a sample schema on an Oracle 10g XE instance that I set up for this purpose.

Recently, I came across the problem that I needed to sort data upon retrieval. Nothing special there, one could say. But what I needed was sorting by:

a.) a custom field, provided by the user
b.) a custom order direction (asc/desc), also user provided

Unless I wanted to use the custom sort direction, HQL did the trick for me. I simply could create a query like:

Sample hql query using custom sort field

Just had to pass the customField parameter before calling the AllHouses query. However, when I try to pass also the sort direction, like in the following figure, any calls to the query fail.

Sample hql query using custom sort field and custom sort direction

At this point, any call to the query fails. The Oracle XE is obviously not able to assign the ASC/DESC sorting direction through a parameter. Hence, I came up w/ the following solution: Criteria queries! This way you can add custom order fields AND custom order directions.

In short words, it is simply done by using the AddOrder method of an ISession object. It takes as a parameter the field (custom provided by you), and has got two methods (Asc() and Desc()) for creating an Order object. The rest should be self-explanatory.

Here is how it works:

Criteria query using custom field and sort order

Simple, isn’t it? One more advantage is, that this way you are a bit more independent from DB-vendor-specific sorting possibilities. nhibernate will take care of creating the correct SQL syntax, no matter what the underlying DB is (as long as it’s supported, of course).

The nhibernate version I used for these tests is 1.2.

Stay tuned to the next time!

Martin

Read Full Post »