Introduction to ORM (Hibernate)

Introduction to ORM (Hibernate)

Hibernate, If you are a Java developer I am sure you heard this name already. Yes! it is one of the ORMs available in the market.

ORM tool or you can say an ORM Frameworks stands for Object Relational Mapping.

If you have never worked with any ORM tool then your project structure will be like this

 

As you have used JDBC for a long time and we know that it is very powerful API for a Java developer it gives a way to connect your application with the database but it still has some problems related to java’s best feature Object Oriented.

Demerits of JDBC approach-

  1. You will be using native SQL queries
  2. In case of changing your database vendor you have to change all your queries
  3. As Java is object-oriented, So you need to manually convert database ResultSet to Java Objects and vice-versa (Database object to Java Object and vice-versa)
  4. Java developer may require database specific knowledge (Database objects, SQL)
  5. State of Java Objects fetched from developers should be managed by developer

ORM tool basically solves the main problem of JDBC API, it maps database objects to Java objects. An ORM makes database operations Object Oriented. A developer can manage database by just handling simple java objects. But this is to keep in mind ORM internally using JDBC to manage your objects.

Benefits of ORM-

  1. Productivity: When using an ORM tool, the amount of code is unlikely to be reduced—in fact, it might even go up—but the ORM tool generates 100% of the data access code automatically based on the data model you define, in mere moments.
  2. Application Design: A good ORM tool designed by very experienced software architects will implement effective design patterns that almost force you to use good programming practices in an application. This can help support a clean separation of concerns and independent development that allows parallel, simultaneous development of application layers.
  3. Code Reduce: If you create a class library to generate a separate DLL (Data description language) for the ORM-generated data access code, you can easily reuse the data objects in a variety of applications. This way, each of the applications that use the class library need have no data access code at all.
  4. Application Maintainability: All of the code generated by the ORM is presumably well-tested, so you usually don’t need to worry about testing it extensively. Obviously, you need to make sure that the code does what you need, but a widely used ORM is likely to have code banged on by many developers at all skill levels. Over the long term, you can refactor the database schema or the model definition without affecting how the application uses the data objects.

One potential downside to using an ORM is performance. It is very likely that the data access code generated by the ORM is more complex than you’d typically write for an application. This is because most ORMs are designed to handle a wide variety of data-use scenarios, far more than any single application is ever likely to use. Complex code generally means slower performance, but a well-designed ORM is likely to generate well-tuned code that minimizes the performance impact. Besides, in all but the most data-intensive applications, the time spent interacting with the database is a relatively small portion of the time the user spends using the application. Nevertheless, we’ve never found a case where the small performance hit wasn’t worth the other benefits of using an ORM. You should certainly test it for your data and applications to make sure that the performance is acceptable.

There are lots of ORM tools available in market

List of ORM:

  1. Hibernate
  2. JPA
  3. Active JPA
  4. iBATIS
  5. IBM Pure Query
  6. See more on Wikipedia

Leave a Reply

Your email address will not be published. Required fields are marked *