by imtringued · 7y ago · 0 comments
Foo.withSession { session ->
session.createSQLQuery("INSERT INTO FOO (bar, baz) VALUES (:bar, :baz)")
.setParameter("bar", "bar")
.setParameter("baz", "baz")
.executeUpdate()
}
vs new Foo(bar: "bar", baz:"baz").save()
I don't need more justification to use an ORM.Right off the bat, its obvious how to do a transaction in the first example but not the second. Its also obvious to use complex SQL functions in the first style and not the second.
Here's your query in knex, a query builder for JS:
knex.insert({ bar: 'bar', baz: 'baz' }).into('foo')ORM's are a bad case of this, because they try to bridge the impedance mismatch between how data is stored and linked in the database and the programming language you are using. This creates a forced model and a whole lot of assumptions and in the end it is still not good enough. Fixing the part of your project that does not fit the ORM's assumptions gets magnitudes harder because the bridge they built is very complex.
Yes, most ORM's allow you to write direct SQL queries, but that does not really solve the problem. You work the way it was intended, or you do not not. If the latter is the case, you will encounter difficulties, no matter the way break the 'rules'.
When you write a technical paper in English, you switch to math equations when appropriate. Math isn't assembly language, its the correct language. It's the native language.
Yes, you can always describe your equations with English prose—and if you're doing simple addition and subtraction, it's probably nicer to write that in English. But once you want to do anything remotely complex, writing it as a math equation is more terse and less ambiguous.