Archive for March, 2009

NHibernate Validator 1.2 alpha3

Download NHibernate Validator 1.2 alpha3 with sources and binaries here

This release demostrate to be very stable and is an update to use it with NHibernate 2.1alpha2. For those who have NH.Validator 1.2alpha1 this is a mandatory update.

Enjoy it !

2 Comments

New NHibernate and NHibernate Validator releases

No Comments

Registering FREETEXT or CONTAINS functions into a NHibernate dialect

Ms Sql Server FREETEXT and CONTAINS functions are used into FullText search capabilities to querying. These functions are of course natives to this particular RDBMS and even comes a with particular structure: they don?t return a value. So far, till NH 2.0, you couldn?t do it because a little parser issue, but from NHibernate 2.1 in forward you?re enable register them.

First of all, we define the new dialect with the new functions in order that when we run a query, NHibernate can recognize those functions and can transform to native-sql, in this case, Transact-SQL.

using NHibernate.Dialect;
using NHibernate.Dialect.Function;

namespace MyCompany.Data
{
    public class MyDialect : MsSql2008Dialect
    {
        public MyDialect()
        {
            RegisterFunction("freetext", new StandardSQLFunction("freetext", null));
            RegisterFunction("contains", new StandardSQLFunction("contains", null));
        }
    }
}

Note that we are using StandardSQLFunction, we can also use SQLFunctionTemplate or implement our ISQLFunction class with all the constraints (ie: parameter number accepted) we need.

Once our new dialect is ready let?s call it from our hibernate.cfg.xml file, then NHibernate can know that this dialect will be inject. Suppose MyDialect is placed into the assembly MyCompany.Data, so the configuration file should look like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="NH">
        <property name="dialect">MyCompany.Data.MyDialect, MyCompany.Data</property>
        <property name="connection.connection_string">
            Data Source=(local)\sqlexpress;Initial Catalog=test;Integrated Security = true
        </property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    </session-factory>
</hibernate-configuration>

Everything is ready, you just have to do this 2 steps and the functions are ready to use it, then we can query using them.

session.CreateQuery("from Documento where freetext(Texto,:keywords)")
    .SetString("keywords","hey apple car")
    .List();

Note the above query is HQL, so NHibernate knows about freetext and can operate with it.

1 Comment

NHibernate and Ms Sql Server 2008: Date, Time, DateTime2 and DateTimeOffset

Ms Sql Server 2008 come with a lot of features talking about Date/Time. Besides the already known DbType.DateTime, now we have new types:

  • Date
  • Time
  • DateTime2
  • DateTimeOffset

NHibernate 2.1 introduce changes to support this types out-of-the-box. This table show all the details about how to configurate your system: classes, mappings:

CLR Type

DbType

Sql Server Type

NHibernate type

System.DateTime

DateTime

datetime

datetime

System.DateTime

DateTime2

datetime2

datetime2

System.DateTime

Date

date

date

System.TimeSpan

Time

time

TimeAsTimeSpan

System.DateTime

Time

time

time

System.DateTimeOffset

DateTimeOffset

datetimeoffset

datetimeoffset

A breaking change was introduced: the old TimeSpan NHibernate type was moved to TimeSpanInt64 (it uses a System.Int64).

When we talk about ?NHibernate type?, we are refering to what you?ve to put into your mappings, i.e: <property name=?OnlineMeeting? type=?datetimeoffset?/> .

To use this features and more you?ve to use the dialect NHibernate.Dialect.MsSql2008Dialect.

The dialect changes include these types and changes to Hql functions that return the current system timestamp.

  • current_timestamp: Now returns the current system timestamp as a DateTime2 value. It uses sysdatetime Sql Server function at background.
  • current_timestamp_offset: New function!. Returns the current system timestamp with the offset as a DateTimeOffset value. It uses sysdatetimeoffset Sql Server function at background.

The idea of this post isn?t to explain every new type and what it can do, otherwise is to show you what have to do in your classes, queries or mappings to configure your date/time types with NHibernate. Besides, to know every detail of these types you can visit this useful .NET Framework Developer’s Guide.

No Comments