Ramblings from the Creator of WilsonDotNet.com
Paul, Thanks for this. I am also in the process of learning NHibernate and am doing so very much in a trial and error fashion due to the lack of real world examples. This will help me quite a bit. I have also find Cuyahoga (http://www.martijnboland.com/cuyahoga/) to be quite a good learning aid. Jerrie
Hi Paul, Thanks for the example, i will check it out. btw. what a links, trying to get google to like you? :-)
Thank you very much. I just tried both the NHibernate and WilsonORMapper versions; they worked both out of the box against the same database. I am playing a bit with NHibernate the last weeks and enjoy your comments about documentation. Your app shows quit interesting details I did not catch before. But wouldn’t it be so much more work to clean up the code and refactor it into different layers? Or is this the real challenge of ORMappers? I will examine it on my own.
Have you come across Cuyahoga? (http://www.martijnboland.com/cuyahoga/) It is a content management system using NHibernate. The author's website also includes some useful tutorials on NHibernate in a real-world application such as this.
Martijn's Cuyahoga is probably the only other real example I've seen, but to me it was too much of a real app and not enough of an example -- not any fault of his, afterall that's the point since it is a real app that he's making with it. As for refactoring into different layers -- I don't think its a challenge at all actually. Its mostly the same principles you would apply in any other layerd app, although easier in my opinion, but of course I left that undone in my example.
Hi Paul Well done! We can never have enough sample applications to show the merits of O/R mapping. This one is especially nice because it covers many of NHibernate's mapping options but it's also pretty small and easy to understand. One remark about the application: you used the IQuery interface but the hql consist of concatenated strings where everybody should really use the parameter options with the IQuery interface (for obvious security reasons).
I was able to make NHibernate work against access using the oledb option.
Hi Paul, Thanks for the example app - I've been looking for something like this for a couple of weeks now. This one looks like it demonstrates a lot of the main features I would like to take advantage of. However, I'm having trouble getting the example app to run. In the LoadContacts() method, when I hit this line: this.ContactGrid.DataSource = session.CreateQuery(query).List(); I get this error: An unhandled exception of type 'NHibernate.ADOException' occurred in nhibernate.dll Additional information: Could not execute query. Any suggestions on what I might be doing wrong? Thanks!
Hey Dale: Hopefully someone more experienced with NHibernate can shed some light on it. The only thought I have is probably not the issue, but I suppose I should ask anyhow -- have you created the database with the sql script? Yea, probably not the issue. :) Good luck, Paul
Yeah, I ran the script to create the db instead of doing it by hand. I haven't been able to get ISession.CreateQuery() to work in either your example or my practice app. Still can't figure it out. If I come up with the solution, I'll post it here.
Hi Paul, I figured it out. Dumb move on my part: for some reason the ORMExample login didn't work for me. I just changed it to a login that does work and voila, I'm in business! Now if I can figure why I'm messing around with computers at midnight on a Saturday night instead of messing around with my wife, I'll be making some real progress. thanks, Dale
Dale, There is probably something wrong with your connection string. Take a look in the log.txt file for more details about the exception.
Link Listing - May 30, 2005
This is one of the few examples available about NHibernate, that actually represent something out of the real world. But there's one mayor thing I don't like about the code. It is impossible to reuse the business model in some other form of presentation layer. That is one of THE things you need when building extendable and maintainable applications.
Its definitely not intended to be a model for best practices. I actually thought about making it a little better in that regard, but I think it would make it harder to grasp the NHibernate usage for some that aren't as experienced.
Very nice to see some more examples of nhibernate applications. We are looking at nhibernate / borland's enterprise core objects (ECO II) and a number of other solutions and haven't really found anything that supports a really good distributed model that can be used say for a smart client application. By this I mean it'd be nice to have a ORM that allowed me to host the domain model on an application server and access it via web services. On the client I'd like to have real objects (proxy) that allows me to make modifications and then commit my modifications using a web service. Rich Strahl describes this requirement better: http://west-wind.com/weblog/posts/368.aspx Also I don't really want to transfer the entire object back over, just the difference. I need to be able to request my own transactions and just have the persistence engine behind the web service coordiate that for me... I don't know sounds like panacea right?
Hi Corbin: Most O/R Mappers (EntityBroker being the exception) assume that you will provide the plumbing for distributed apps, whether it be web services or remoting, just like you would in any other case -- and its not that much different, so I don't really see the problem.
hiya, If so, were you able to use the appConfig connection section "as is"? I am experiencing connection probs :-( If somemone can confirm that the conenction should work as is, then that'd be a help. cheers
It should just be a simple matter of getting the connection string correct -- especially if you have a named instance for Sql 2005.
Thanks, at least I know where I am going wrong.I have tried integrated security etc in the connString.Still no joy. Could someone who is using the sample app with sql2005 email / post their nHibernate dbConn section?I apologise for this slightly off-topic :-( cheers, berriolas@yahoo.com
Thanks for the 'real' example Paul. Was beginning to doubt whether I'd find anything resembling a real-world scenario for nHibernate on the web.
Thanks for the nice example. But can you tell me, from where can i get the documentation on NHibernate.
Uh, my guess would be the NHibernate site.
Hi,Paul Thanks for you work. To Wilson you can get the documentation at http://wiki.nhibernate.org/display/NH/Home
This is the first NHibernate (or Hibernate) example that I was able to run successfully. I ran it using VS.NET 2005 using MSDE for the database. The only change I made was to update App.config with the database instance I configured for this example (as opposed to using the default instance). The example is sufficiently rich in features to make it a good basis for further study in understanding the Hibernate configuration and programming details. Thank you very much for taking the time to post this example.
HOW CAN WE USE NHIBERNATE WITH SQLSERVER 2005....I AM HAVING A DRIVER ERROR...SECONDLY SOME TIMES I GET AN ERROR ABOUT .HBM.XML NOT FOUND....HELP
You shouldn't need to do anything differently for Sql Server 2005, other than change your connection string if that's necessary on your install. As for hbm.xml errors, I would think that you would get more help on the NHibernate forums, from regular NHibernate users, instead of asking me. I'm pleased to offer something to the NHibernate community, but keep in mind that I do have my own ORMapper that occupies my time.
Hi, Paul Thanks for your work, it's very interesting and much more completed than other. I would ask a question. In the example you use always session.CreateQuery so you use the IQuery interface. So you must make the query string (SQL string). In other example I have seen session.CreateCriteria. In the first you must know the DB structure and think in Relational mode (SELECT, JOIN, OUTER JOIN and so on). In the second you think in more OO. It's true? It is better use session.CreateCriteria? I'm novice in NHibernate but I think that I must masquerade the DBMS. A real application with NHibernate don't must have SQL-spagetti (also for security reason aka SQL Ingnition)
First, keep in mind that I'm not an NHibernate expert since I have my own ORMapper -- so I simply used what I did to make it work and don't claim its "best". But having said that, I actually do like to think in Relational mode and don't view that as a problem -- but I agree its nice that an OO viewpoint is also supported. I do the same thing with my ORMapper -- I tend to build Sql strings, since that's my viewpoint, but the ORMapper also supports OPath for OO thinking. For my ORMapper though I do have a QueryHelper that helps me build those Sql strings, so I don't have to hard-code table and filed names, and I can also make sure my Sql strings are "clean" to avoid Sql injection. I don't know if NHibernate has a similar thing to avoid such hard-coding and Sql injection, so while that's not a problem with my ORMapper, it may be more advisable with NHibernate to avoid it.
Thanks, Paul Wilson
man, seems in connection string shall change "User=ORMExample" to "Uid=ORMExample"
Content on this page is severely out of date. Go to
I have been searching for an example of NHibernate and was glad to find this. I'm kind of a newbie, so sorry but how do you get the database data? Thanks.
I have been looking for a good example for long time now - i found yours and it were working the first time - damn good work! :)
Pingback from Property with Formula (generated=always) generates invalid SQL for post-insert Select | rapid-DEV.net