3 ms·
I was pretty excited when I originally learned about ORMs. But I quickly found that their usefulness is very limited in the enterprise, where most of my career
by halis 7y ago
I was pretty excited when I originally learned about ORMs. But I quickly found that their usefulness is very limited in the enterprise, where most of my career has been spent.
You will likely find, as I did, that enterprises literally have thousands of databases of all kinds and many crazy data models.
They have tomes of huge SQL procedures that are still to this day running their business processes. Maybe even a nice older gent that is the only one that really understands how any of that shit works?
If this sounds familiar to you, then don't waste too much time on ORMs. In my experience, they're only good for mapping out your POJO or POCO objects and performing simple CRUD operations.
Use them to stand up simple REST routes that do CRUD on your entities. That can be the API that you use to do development and testing, and possibly even use those CRUD routes to do larger business operations.
But in reality, the business "requirement" is going to come down to you, the developer, in the form of some old terrible blob of shit SQL. Two thousand lines. Ten thousand lines.
You're probably not going to bother deciphering all that and re-writing it. The ORM is going to barf all over itself if you even try to use its API to execute that query. Even if you could express all those joins and case statements via the ORM, it is going to pick an execution plan where the query finishes sometime after the next Hanukkah.
At this point, don't bother with the ORM. Drop down to some driver where you can just run raw SQL and run the raw SQL.
Create a REST route that just calls this whack query directly.
This all may sound cynical and it is. But I know what I'm talking about. Do yourself a favor and remember what I said.
- rambojazz 7y agoYes this is my experience as well. ORMs are really nice for simple queries, not so much when queries start to get more complex.
- imtringued 7y agoI have written several queries that span hundreds of lines, primarily for "advanced" search functionality that lets you search by a huge array of potential parameters. Without using the query builder provided by the ORM the code would become intractable to mortals quickly. Having several dozen different parameters that are only set under certain special conditions is much easier if you can use your regular programming language to evaluate those conditions as opposed to writing everything in one big SQL statement even with named parameters. Writing code like this here really gets old: q = query(SELECT a FROM table WHERE (:PARAMETER_1_DISABLED OR table.field < :PARAMETER_1), ... 20 other parameters) q.setParameter(PARAMETER_1_DISABLED, condition) q.setParameter(PARAMETER_1, parameter) .. 20 other parameters compared to just Type.list() { if(condition) { field < parameter } .. 20 other parameters } Also please don't tell me to avoid this problem by concatenating the SQL conditions to dynamically create the query... The end result is still ugly and might even introduce security problems.