Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we really need to use hibernate for our Java code? [closed]

Tags:

java

database

As a beginner to Java-database projects, how does one decide when one should use Hibernate in Java code instead of simple jdbc?

like image 513
bread butter Avatar asked Aug 30 '12 02:08

bread butter


People also ask

Is Hibernate really useful?

Hibernate– Hibernation is a good option when you know you won't use your laptop for an extended period of time and you are unsure of when you'll have the chance to charge it again. This is also a good option for desktop users concerned about power consumption as it doesn't use as much as sleep mode.

When should I use Hibernate and JDBC?

Hibernate is mostly considered for complex apps. JDBC is a much better option, if: If an app is using a simple database that does not require to migrate, or. If the application needs data to be stored in database tables that won't require object-mapping to 2 or 2+ table versions.

Why we are using Hibernate instead of JDBC?

The short answer on the fundamental difference between JDBC and Hibernate is that Hibernate performs an object-relational mapping framework, while JDBC is simply a database connectivity API. The long answer requires a history lesson on database access with Java.

Is Hibernate Java still used?

No, there is no way that Hibernate is deprecated. There is the JPA which is a persistence specification and Hibernate implements it. Also Hibernate has its own advanced features that JPA does not have and that's why Hibernate is the main source of new features that are added to the JPA standard.


2 Answers

Use hibernate if 1 to 4 are satisfied

  1. If you are designing the database schema and your domain objects afresh.
  2. You envision that your domain objects and your schema will be in sync, for example an automobile vendor application where your vehicle objects are likely to be close to your table structures. Just an example.
  3. You really want to use an ORM and tie your objects closely to schema.
  4. Want to use a ORM framework to handle connections etc.

Else

I would recommend ibatis. This is still an ORM framework but leaves flexibility in handling schema and domain objects separatley. It offers more flexibility in handling queries etc.

Spring jdbc also has good features offering flexibility. But i feel it still not near to an ORM. But spring does offer templates to handle orms like hibernate and ibatis.

like image 55
techuser soma Avatar answered Sep 22 '22 14:09

techuser soma


Basically, if your application needs to store some data, you will probably want to use an ORM (e.g. you built some business app for your company and you need to track users and accounts). If your application is your data, then you will probably want to use JDBC (e.g. you built a data warehouse for your company).

as an additional note, it's perfectly reasonable for an application to use both ORM and JDBC. if, for instance, you were building a webapp which was allowing users to access your data warehouse, you might manage the user/account info using ORM but use JDBC to interact with the data warehouse.

like image 23
jtahlborn Avatar answered Sep 21 '22 14:09

jtahlborn