Hi, I am using hibernate for the first time for one of my projects. One of the entity is Education(StudentObject, CollegeObject, MajorObject, degreeString). To insert Education object into database, i've to load student, college and major object from database and then instantiate it and persist. How is this better than just plain sql(insert into..) query? Wouldn't sql queries be faster, as there is no overhead of loading 3 diff objects from database?
Yes. Sql queries are faster.
All ORM frameworks have this overhead.
The thing is that an ORM framework should increase developers productivity and the overhead of SQL queries should be managed when and if it really makes things run slow. In my experience this is really rare. When needed you can usually fall back on SQL. The majority of applications are doing fine with an ORM framework.
If I write a small application I would not even use an ORM framework. But it is a chance for you to learn.
Wouldn't sql queries be faster, as there is no overhead of loading 3 different objects from database?
Correct me if I'm wrong but from where do the IDs of the StudentObject, CollegeObject and MajorObject come if not from the database? And if they're some kind of reference data, they're perfect candidates for second level caching (and this would mean no database hit at all).
Regardless of your answer, here is my opinion on using an ORM such as Hibernate for a typical CRUD application:
- It reduces development time by removing all the annoying code required for manual CRUD.
- It provides nice features such as lazy loading, second level caching, query caching
- To my experience, it generated better SQL than the average developer
- For 80% of the use cases, it will just works fine
- And even after spending time to tune the other 20%, you'll still be done faster than without ORM.
To sum up, the whole point is to improve the productivity of developments and my experience is positive from this point of view. In general, it performs really well, actually even better than custom code (thanks to lazy loading and 2nd level cache). For some use cases, the performances won't be "as good" as with custom SQL though. But tuning is possible.
Back to your question, I think the benefits are worth a possible overhead on some use cases (although I'm still not convinced your example is a good one). But do you actually have a performance problem?