views:

446

answers:

5

This is something I deal periodically with, and first time it was me who needed conviction. Luckily, I just tried, made extra effort to learn and thanks to this book, Spring support, and Hibernate I won't start a project without considering JPA. But not everyone is willing to go extra mile that often (just in anything we deal with I guess). So how and what to say/present/explain/argument to at least shift their attitude toward ORM?

Related: Convincing a die hard DBA to use an ORM for the majority of CRUD vs Stored Procedures, View, and Functions

+2  A: 

So how and what to say/present/explain/argument to at least shift their attitude toward ORM?

Ask if they want to keep writing the same boilerplate SQL JDBC CRUD code for every new entity type you add to your application.

Without an ORM solution in the data layer, each new entity class requires N units of work to make that entity available through the data layer (DAOs, CRUD code, stored procedures, writing queries, etc.).

ORM transforms that N into a fraction of itself, assuming that adding new entities then requires you to just add another mapping for that entity in metadata.

matt b
great argument that is hard to dismiss, but usual counter-argument would be 'yeah, but beyond CRUD I can't use real database stuff, etc.
grigory
A: 

Less boilerplate and extra security. You get to save your fingers and junior programmers have less chances to introduce SQL injection vulnerabilities which I find very important.

palto
+1  A: 

I think this answer from your related question link nails the underlying issue. If the database's purpose is tightly coupled to your application, and it doesn't really have a separate identity, then ORM makes a ton of sense.

If, however, your database represents a larger picture and your application is one of the many applications that accesses data, using ORM can have potential negative impacts on the coupling between your application and the database and the need to change the database over time.

But to some degree you can answer your question yourself - what objections did you have? How were you convinced the first time?

Yishai
I had all possible objections imaginable because my experience was heavy on a side of stored procedures, and then JDBC/Spring/EJB (pre EJB3). I think the only argument for I had was is that the new technology can't be that bad if so many applications began to use it in last couple of years. Then I started realizing that a lot of my own resistance comes from old-style approach to database development and inability to link ORM with database design, relational theory, optimization, best practices. After I made an effort to use ORM with all that in mind it was a breakthrough.
grigory
+1  A: 

OK, I'm going to play the devil's advocate here.

Stored procedures are a layer of abstraction. JPA requires you to know the underlying table structure. Stored procedures/functions mask that; you only need to know procedure or function to call and what its return type is.

JDBC makes calling stored procedures very easy, using the Connection object's prepareCall methods.

Stored procedures also add a layer of security: The application itself usually cannot manually modify the tables, only call the procedures that do the modification. The SP/SF should also be validating the arguments passed to it.

The major drawback with stored procedures is getting data back out... you need to create your own facility to map arrays and structs coming back to Java objects, usually with a type Map and special programmatically created objects implementing the SQLData interface.

R. Bemrose
Then let me play the anti devil's advocate: SP are non portable, harder to test, to refactor, they make reusing the logic harder... For me, the downsides outweighs the benefits. See also [Who Needs Stored Procedures, Anyways?](http://www.codinghorror.com/blog/2004/10/who-needs-stored-procedures-anyways.html)
Pascal Thivent
major drawback of stored procedures is that they distribute your business logic across fundamental tiers; and the tiers do not cooperate all too well because we are limited to legacy JDBC; end result is doubling the effort or ditching business layer (which is not really possible) thus leaving it incomplete and inconsistent. Every argument about security in databases I can dismiss immediately be referring people to best practices - whatever technology they intend to use. But honestly, I don't want to turn this into JPA vs. JDBC discussion :-)
grigory
@grigory: These are likely the arguments you'll have to counter, whether or not you like it.
R. Bemrose
I wouldn't dismiss SPs outright. For teams that are heavy on SQL development, batch processing, database integration they are indispensable. A lot of legacy code (especially MS-SQL) base multi-tier development with JDBC/ODBC on stored procedures. But I would insist on thinking about them as a legacy tool...
grigory
+2  A: 

I'll assume that you actually have an object model that's richer than mere DTOs and not simply a 1:1 correspondence with your relational schema. If not, you don't win the argument.

You win if the SQL generated by Hibernate is at least as efficient as the hand-coded stuff that's in the stored procs.

You lose if the stored procs can be optimized to perform better than Hiberate-generated SQL.

You lose if the database is shared by other apps that depend on the stored procs. You can't move the logic to Spring and the middle tier as easily.

You have a better case if your app owns the database.

Since you're already using Spring, that suggests that you have a DAO layer that takes advantage of Spring Validation. PreparedStatements are already used underneath, so SQL injection is as unlikely for Spring as it is for stored procs.

duffymo
it's always richer - that is why it's called ORM(apping). Given that domain model contains at least one relationship (of any kind) it's richer than relational schema.
grigory
Not if it's a 1:1 mapping it's not. "Always" is too strong in this case, especially since the OP didn't say one way or the other. And why is one relationship in the object model richer than a foreign key relationship between two tables? I'm not following.
duffymo
let me explain. Database relationships are defined using foreign keys. When manipulating single table there are just foreign keys present. When manipulating single entity there are always lazy or even eagerly initiated relationship entities.
grigory
if ORM SQL is just 10-15% less efficient then should I dismiss it? Does 10% decrease in performance outweighs 50% gains in productivity? If we have 50% difference then would the right question be reviewing ORM domain model and design - it is likely inefficiency originates there?
grigory
@grigory - your explanation makes no sense whatsoever.
duffymo
"Whatsoever" is too strong in this case. Please, do not look at the whole domain model and the whole relational database when comparing. Look at given entity and corresponding relation (table) - the relation is limited to its data and foreign key definitions, while the entity comprises rich graph of objects.
grigory
The data and its foreign key definitions can be at least as rich as entities, otherwise ORM wouldn't work. "No sense whatsoever" was referring to this phrase: "When manipulating single entity there are always lazy or even eagerly initiated relationship entities."
duffymo
Of course ORM would not work if it can't read relational data! Come on, the point I make is actually simple: graph of objects (contained by single entity) is richer than relational database tuple. I guess we can only agree to disagree from this point on.
grigory
You said "always". I disagree - relational can be as rich as ORM if it's 1:1. And if it's not in relational, but it's in objects, then it's not persistent. You can map M tables to N objects, when N >= M, but the data is the same in either case. "Richer" is a subjective judgement.
duffymo