posted on Thursday, October 07, 2004 2:16 PM
by
anoras
Try doing this with SQL!
While researching for an in depth article about NHibernate I discovered a cool feature in the Hibernate Query Language (HQL). Because the framework supports polymorphism, you can retrieve every single persisted entity in the database with one simple query:
1Configuration cfg=new Configuration();
2ISessionFactory factory=cfg.BuildSessionFactory();
3ISession session=factory.OpenSession();
4IList objects=session.Find("FROM System.Object");
After running this code snippet, the list will include every single entity in the database casted to System.Object. This might not look particularly useful in real-life scenarios, but what if you exchange System.Object with the name of a base type in your class hierarchy? This would enable you to retrieve all varieties of an object with one single query.
Using System.Object in queries also very handy when working with test data. I use NHibernate to populate the database with data in the SetUp handler in my NUnit tests and then I clean up after myself in the TearDown handler by deleting all entities from the database. Like this:
1session.Delete(“FROM System.Object”);
Now try doing that with SQL!