Reputation: 5532
Coming from a Ruby on rails background I am used to Active Record and running migrations (for almost everything that requires database changes). I understand that Grails is different but how does exactly Hibernate fits in ?
So this is how I see things,
Active Record (ORM for RoR) = GORM (ORM for Grails)
Where does Hibernate fit in ? Rails talks to the database without Hibernate and why does Grails need hibernate?
Also is it possible to use Grails without Hibernate?
Thanks
Upvotes: 0
Views: 1224
Reputation: 562
Where does Hibernate fit in ? Rails talks to the database without Hibernate and why does Grails need hibernate?
On a lighter note, this is similar to saying "Grails talks to database without ActiveRecord, so why does Rails need ActiveRecord" :)
ActiveRecord and Hibernate just happen to be the default implementations of database access for the "Model" layers of RoR and Grails respectively.
Actually for Grails, it's more correct to replace "Hibernate" with "GORM" in the above sentence. Like even the OP rightly mentioned, they fulfil the role of an "ORM component" in the two frameworks. The point that GORM is defined at a higher level of abstraction than a typical ORM, is already covered by @Sudhir and @Joshua in their answers. But, of all the possible implementations, Hibernate is the default one; so to assume that "it is the only way" is not unusual.
(As a sidenote, GORM may no longer be the acronym it used to be, where R = Relational, because there are many non-relational implementations today. It's high time now that it becomes a word on its own: "Gorm"; just like "Ajax" is no longer the acronym it used to be :) Or maybe a new acronym GDM for "Grails Data Mapping" – the Github name of the project?)
So the short answer to your last question…
is it possible to use Grails without Hibernate?
…is "yes".
But if you were to ask "is it advisable to use Grails without Hibernate?", the answer would be: "it depends… but NO for practical purposes!"
If you're using a non-relational data store, then it's a no-brainer. You don't have much of an option; each non-relational data store has at max 1 implementation of GORM. In fact, you might even be forced to not using GORM at all (depending on whether an implementation is good enough, or even exists!). Btw, yeah Grails can be used without GORM too! Whether it's worth it, is a decision to be made for the particular project.
But if you're using a relational database and still don't want to use Hibernate, you could use the JPA implementation of the GORM using the gorm-jpa plugin. But it seems to have not progressed beyond a milestone release since 2012. And I have never used it, to be able to comment on whether it works flawlessly (or at least as good as Hibernate).
Or if for any reason you find that the Active Record pattern (RoR's default implementation) suits better for your application than the Data Mapper pattern (this is what Hibernate follows), then you could probably use something like JavaLite or ActiveJPA or even Spring JDBC templates (mentioned by @Vivek, but you'll have to first implement the ActiveRecord pattern with it), and then either use it standalone (outside GORM) or maybe even implement your own GORM implementation of the ActiveRecord pattern using one of them. Who knows, that might make it easier for other Rails developers to pick up Grails :)
In other words, the safest and quickest way to use Grails with a relational database is to go with the Hibernate implementation of GORM.
For migrations, Liquibase is a great tool. There's even a Grails plugin.
Upvotes: 1
Reputation: 4096
GORM is an abstraction layer, it is implementation neutral, there can be multiple gorm implementation, for example, gorm for hibernate, gorm for mongo db or gorm for cassandra.
Now coming to your question "Where does Hibernate fit in" Grails(or GORM) does not implement its own ORM, it provides api's for plugins to extend it, the plugins leverages existing ORM frameworks such as Hibernate or JPA and delegates the actual work of object relational mapping to those frameworks. So here Hibernate is the one who actually does the job, gorm provides an easy to use api on top of those existing frameworks.
There exists multiple implementation of gorm, hibernate plugin is one of these, but there's also gorm for mongodb plugin. and if you want, you can create your plugin to support gorm for some other persistence technology.
Upvotes: 0
Reputation: 4268
GORM also uses Hibernate under the hood i.e. GORM is built on top of hibernate. There are 2 approaches that you can follow in Grails:
Also is it possible to use Grails without Hibernate?
Yes, it is but is not preferable. Obviously you can use JDBC templates provided by the spring in Grails.
Upvotes: 0
Reputation: 24776
It's possible to use Grails without Hibernate. This is due to the way that GORM is designed. GORM is an API layer that abstracts record persistence. There are various implementations, Hibernate being one of them.
Upvotes: 1