How to skin a live monkey by using just your teeth

... has nothing todo with what im about to tell you. No, really, it dosent. Although, its about the same - a quick how to on how to write SQL JOIN's with nHibernates "super-shitty" HQL


Say heres your bog standard JOIN statement that you want to run:

SELECT * from MODEL INNER JOIN ModelClassTypeMap ON Model.Id = ModelClassTypeMap.ModelID
WHERE ClassTypeId=2 ORDER BY HighestNewRating DESC

You cant simply just rewrite that in HQL. HQL dosent use the "ON" directive. So you have to do multiple FROM statements, then tell it what you want to select. Sound easy? Welp, it isnt:


SELECT model FROM Model model, ModelClassTypeMap classTypeMap WHERE
model.Id = classTypeMap.Model AND classTypeMap.ClassType = ? ORDER BY classTypeMap.HighestNewRating DESC

To me this just seems retarded. You cant copy the sql that youv spent minutes perfecting, but you have to go and rewrite the whole thing so a 3 year old blind child whos been on cocaine for the last 6 months can understand it. Why not just do it so that you can reuse queries that you already have, instead of having to rewrite the whole way you write them? HQL is a poor mans SQL, its shoddy and flaky, and hardly documented.

Happiness is a warm gun.

You can Leave a comment here!

Comments

  1. Jon 10/25/2007 12:39 PM
    Gravatar
    yeah, the documentation for NHibernate is pretty scarce, however it is basically a direct port of Java's Hibernate so things like HQL syntax, mapping syntax etc are the same - and well documented.

    also if you're a HQL hater it's worth looking at hibernate/nhibernate's DetatchedCriteria model, you can write stuff like

    DetachedCriteria where = DetachedCriteria.For<Customer>()
    .Add(Expression.IsNotNull("DateAdded"));
    where.CreateCriteria("Details")
    .Add(Expression.Or(Expression.Like("FirstName", searchText), Expression.Like("LastName", searchText)));

    - this lets you do a JOIN query across Customer and Details. those strings mean it's probably still not as statically typed as i think you'd like though, but i hope it helps.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.

Please add 5 and 2 and type the answer here: