Geeks With Blogs
Łukasz Kuryło's blog NHibernate
Using stored procedures with NHibernate
NHibernate has many ways to create and executes queries. With one of them* - the Named Queries we can execute our manually created stored procedures. This example is dedicated to SQL Server. This is important to notice, because this code is database specified. That means, for each database the query can look diffrent. E.g. in SQL Server we call the stored procedure by using the syntax: exec <procedure>. In Oracle it is: call <procedure>, so we must remember it. Let's assume we have a ......

Posted On Wednesday, August 24, 2011 12:12 AM

Simple wrapper for ISession and ITransaction
In my previous post I have used a OperationsInTransaction.Exe... method. This is my simple wrapper for the NHibernate ISession and ITransaction method. Normally when we want to execute some query in the transaction we must write this piece of code using (ISession session = SessionFactory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { try { //some query transaction.Commit(); } catch (Exception) { transaction.Rollback(); } } } Unfortunatelly we must write it every ......

Posted On Wednesday, August 17, 2011 1:51 PM

Using NHibernate with xml column
I have a simple database table with only id and one xml column as shown below. In this column I have a list of available extensions saved as xml. public class FileExtensionsList { public FileExtensionsList() { Id = Guid.NewGuid(); } public virtual Guid Id { set; get; } public virtual XmlDocument Extensions { set; get; } } This xml I'm not creating manually. I have a custom entity which I serialize to xml: [XmlRoot("FileExtensionsLis... [Serializable] public class FileExtension { [XmlAttribute] public ......

Posted On Wednesday, August 17, 2011 1:35 PM

showing a sql generated by NHibernate on the VS build-in console
After done mapping for my entities I wanted to test the basic CRUD operations. I stuck with a problem of inserting a data to tables in many to many relation, so I wanted to look at the generated sql by the NHibernate to find out, what is really generated to investigate the problem in my mappings. The NHibernate have a property called show-sql to show the generated statements. I enabled it in the config: var configure = new Configuration(); configure.SessionFactoryNam... configure.DataBaseIntegrati... ......

Posted On Tuesday, July 12, 2011 11:36 PM

[NHibernate]Mapping a string field as nvarchar(MAX) in SQL Server using mapping by code
I'm diving in the NHibernate 3.2 mapping by code recently. I throught that, mapping a string with specific max length to the appropriate database table will be easy, but it wasn't. I lost some time to figure it out. At the beginning I mapped a string field in that way Property<string>(x => x.Title, x => x.Length(Int32.MaxValue)); After generating the tables in database with my schema, unfortunatelly this field was generated as a nvarchar(255) and I didn't know why. We can deal with it ......

Posted On Monday, July 11, 2011 6:50 PM

Returning Count(*) with NHibernate HQL
I wrote in current project typical funcion to count the number of elements in db for specific criteria using NHibernate HQL: IQuery query = Session.CreateQuery(@" select count(t.ID) from Post p join p.Tags t where t.Name=:tagName and p.Blog.Name=:blogName ") .SetParameter("tagName", tagName) .SetParameter("blogName", blogName); int result=query.UniqueResult&l... I was very surpriced to see that I stuck with System.InvalidCastException: Specified cast is not valid. Solution for this was to ......

Posted On Tuesday, November 2, 2010 8:52 AM

Copyright © Łukasz Kuryło | Powered by: GeeksWithBlogs.net