Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would you use NHibernate for a project with a legacy database, which is partly out of your control?

For me the answer is currently: No, I would use iBatis, because NHibernate is a pain, when the database model and the object model are not in synch. If I don't have full control over the database I end up with a lot of work.

Why do I ask?

Well, first of all: I never used NHibernate. I just know it from the surface. I have read about the advantages of iBatis for legacy databases.

Second: Recently I had a discussion with someone who worked with Hibernate (jep, without 'N' before Hibernate). He told me that the ORM frameworks are now pretty advanced and advocated Hibernate. Since I was not interested in NHibernate, I didn't keep track of the recent developments.

Maybe I its time to rethink my answer, or not?

like image 695
Theo Lenndorff Avatar asked Jan 17 '09 22:01

Theo Lenndorff


2 Answers

iBatis is certainly easy to map objects to legacy database systems.

More recently NHibernate 1.2 and 2.0 have a feature set that may make you rethink iBatis.

NHibernate works with composite keys, which can occur frequently in older databases, they aren't always pleasant to work with but support is there for this.

NHibernate can utilise Stored Procedures for CRUD operations on entities, also database views.

Collections can be custom stored procedures or SQL queries. Collections can use the property-ref attribute when the Foreign Key relationship doesn't map directly to the Primary Key on the other side.

Some of these features may take away from the performance/power of nhibernate, ie Lazy Loading with property-ref doesn't work (at all?), but is most cases there are reasons for this.

Other points: (which aren't really related to your legacy database but still can help decide on a technology choice)

The Nhibernate community appears much richer than the iBatis. I'm on both lists and the volume of support for NHibernate is quite large compared to the iBatis group. So support should be easier.

Also there is a growing amount of contrib/3rd party tools for NHibernate. Things like The NHibernate Profiler, the Nhibernate Query Analyzer, NHibernate Contrib, Fluent NHibernate to name a few.

Perhaps you can expand on what advantages you believe iBatis currently has. NHibernate has certainly been quite active recently and has gained many new features, a lot of which do assist in legacy/hard to modify schemas.

And to answer the question, yes we do use NHibernate with legacy databases that have awful relationships, composite keys, broken relationships. We still also have a small amount of code based on iBatis. We no longer write any more iBatis code though.

like image 136
Jafin Avatar answered Sep 23 '22 23:09

Jafin


Yes, consider NHibernate. It's the gold standard for a reason. I have heard that iBATIS supports crazy mapping possibilities, but with NHibernate's IUserType you can map anything, even really strange columns.

@Ahmad, the entire point of ORM is to prevent a tight coupling between your objects and your schema. If you have this problem you're doing it wrong.

Also, with NHibernate there are plenty of options for custom queries, formula properties and stored procedures. HQL is extremely powerful and Criteria is flexible.

I think you'll be doing your clients a disservice if you don't at least spike NHibernate.

like image 30
Matt Hinze Avatar answered Sep 24 '22 23:09

Matt Hinze