Tuesday, January 14, 2014

Spring Data Should be Renamed to Spring Datta ?

In this Google era, everything should be searchable and Spring Data definitely needs its re-naming ceremony and some cool looking name ! In fact, many of Spring sub-projects needs it's own name like Apache has been doing.

So "Datta" or Dutta is short form of "Dattatreya" and originates from India and it means "God". Spring Data deserves that stature anyway, doesn’t it ?! :)  And if you you don't want to be religious, the same Datta has been transformed as powerful street fighter Dhalsim ! http://en.wikipedia.org/wiki/Dhalsim . I have found Spring Data so powerful, I can't stop writing about it in this usually-developer-productivity-related blog !


Spring Data
For them, who have not used or read about Spring Data. It is name of the umbrella project for everything related to Data. So, Spring Data JPA is working with data using JPA. And Spring Data MongoDB is same shell but works on MongoDB.

Flip-phone Vs Smart-Phone

After reading about Spring Data, I remembered start of my software development career with Java Prepared Statement ! It's like remembering your flip-phone after handling your new S4 or iPhone 5s.
I remember those arguments to pStmt (you remember this variable name ? ) and how I screwed with sequence of arguments ! :)

Then came Hibernate where I could make use of my Object oriented mind to map database tables to objects and I was partially relieved from switching my mind from OO to Relational !

New Logo ? Spring Datta ?
You might ask what Spring Data brings compared to Hibernate/JPA. It goes beyond or on top of JPA/Hibernate where you get far more than only mapping database entities to java objects. Those basics are done by JPA/Hibernate as usual.
Here are some Datta like features of Spring Data ! I am usually excited about any framework's feature which reduces the time of development drastically ! So I would mostly focus on "productivity" features of Spring Data.

Forget about SQL , remember the English !

This is the feature I was really impressed and it got me tempted to try to believe it ! :) Not that I don't believe the new spring.io documentation, in fact it's way too improved and I love those getting started guides ! Short and sweet, plus their sweet requirement in the list of "What you will need"- "You need to have 15 minutes" :)

So what I tried was this. As per documentation, just write the method names in right way, you will get implementation automatically ! So here is my simple method name and it worked so nice !

CustomerRepository customerRepository = context.getBean(CustomerRepository.class);
customerRepository.findByAccountsInternalType("PremiumChecking");

Here each Customer has one or more Accounts. Account has "InternalType" as one of attribute/column which may indicate say Checking, Savings , PremiumChecking etc. So this little piece of English "customerRepository.findByAccountsInternalType("PremiumChecking");  means find customers who have accounts with internal-type equal to PremiumChecking. Wow, this has saved me so much joins or criteria I would have to write if I was using just Hibernate or JPA. So you can go endless findByTable2Colum2Table3Column4 and so on..

And of course you can use all the grammar. For example words like "Between" , "Like" and what not ! See here  http://docs.spring.io/spring-data/jpa/docs/1.2.0.RELEASE/reference/html/#repository-query-keywords

I have used Spring Roo extensively and was amazed by it's auto-generated finder methods but it could never do cross entities join but Spring Data can do it !

Many modern databases are joining under Spring Data umbrella :

As I mentioned above, Spring data is umbrella project and all this "English Vocabulary" can be used on many databases like MongoDB, Neo4J , Hadoop , surely many other databases joining under the umbrella to handle the rain of data from all directions.
Spring data not only works with databases but the same interface can be used for distributed memory like Gemfire too.

Talk in DSL as well :

Spring data supports QueryDSL as well to ease writing complex finders in your code which is more readable. Here is the example by queryDSL itself.

List<Customer> result = query.from(customer)
    .where(customer.lastName.like("A%"), customer.active.eq(true))
    .orderBy(customer.lastName.asc(), customer.firstName.desc())
    .list(customer); 

Add REST on top : There is another project called "Spring Data REST" based on Spring data. I dont know much about it at this time, but I am sure it adds effortless REST support on all what Spring data can do and makes your "modern webapp" development super easy.

Conclusion : One can be super-productive using Spring data ! Go for it ! :)

My tweets on similar topics