Sunday 27 April 2014

Hibernate learning made easy by sample code snippet

If you wish to quickly get started with Hibernate, then this post is to provide you the sample code snippet for learning Hibernate. Hibernate is a ORM (Object / Relational Mapping) framework and provides implementation of JPA (Java Persistence API) specification. So first of all, let's start with what is ORM!

About ORM?

ORM tool serves as bridge between application  in object oriented language (e.g. Java) and relational database (e.g. MySQL). ORM works by transforming data from one representation to other i.e. from programming language’s application objects to database table rows & vice versa. With help of conventional relational database management system, ORM provides virtual object database. It maps,
  • Classes                     ->     Database Tables
  • Properties/fields        ->    Table Columns
  • Runtime  Objects      ->    Table Rows

Advantage of ORM:
  • Can reduce the amount of code you need to write  (30% then JDBC)
  • Corresponding boost in productivity (can be drastic)
  • Can make your application maintainable because of fewer LOC
  • Can often combine second level catch to improve the performance
  • Vendor independence (switching from one RDBMS to other becomes seamless, as it just requires negligible configuration change in application code)

Disadvantage of ORM:
  • May be difficult to tune query
  • Performance of tool may affect your application performance, if not used appropriately

In a nutshell, with ORM gain is so great in typical application that you can’t ignore it. ORM works well in 90-95% of cases. Next, how to get started with learning Hibernate!

Hibernate Quick Start Guide

What are learning objectives of Hibernate?
  1. Hibernate tooling and project setup
  2. Establishing connection between Java application and RDBMS using Hibernate Configuration (hibernate.cfg.xml), SessionFactory and Session
  3. Different Hibernate mapping options between Java Entities and RDBMS tables
  4. Load, Get or Query records of database in the form of Java objects by using Hibernate
  5. Perform Save, Update, Delete operations in database using Hibernate Transactions
  6. Learn to use Hibernate annotations instead of xml configuration
  7. Learn advance topics in Hibernate - caching, batch processing…
  8. Leverage code further using Spring framework and Hibernate integration
How to get started with Hibernate learning?
  1. Download sample code snippet for hibernate learning made easy, which is available @ https://github.com/tirthalpatel/Learning-Hibernate
  2. Follow instructions of "README.md" file for project setup.
  3. Now run and explore code of - Hello World tutorial.
  4. In the similar fashion, learn more hibernate fundamentals - O/R mapping (mapping component, collections, entity relationships, inheritance...), Querying, Annotations...

Hibernate Best Practices
  • Mark all the associations as lazy by default
  • Use JOIN fetch mode for loading lazy associations, rather than calling getter method of association
  • Use <component> or @Embedded for better performance in OneToOne relationship
  • Use transactions for save or update operations only
  • Always close the session in finally block
  • Use detached objects in multi-tiered applications
  • Consider abstracting business logic from hibernate, use Dao pattern and Factory pattern
  • Use Spring or EJB3 compliant application server for declarative transaction demarcation
  • More best practices…


Also Refer

3 comments:

  1. crowdsourcehttp://www.incruiter.com recruitment agency.

    We ’incruiter’ provide a uniquerecruitment agencies platform to various committed professionals
    placement consultancyacross the globe to use their skills and expertise to join as a recruiter and
    interviewer to empower the industry with talented human resources.Searching for the right candidate is never easy.
    job consultancy We use crowdsource recruitment to find right talent pool at much faster pace.
    Our candidate search follows application of a rigorous methodology, and a comprehensive screening to find an individual
    whorecruitment consultants is not only skilled but is also the right culture fit for your organization.
    Our interviewers are best in the industry,staffing agencies being experts from various verticals to judge right
    candidate for the job. They interview candidates taking into account primarily defined job specification of our clients and targeting
    them for needs of the organization.Thinking about payment?placement agencies Don’t worry, you pay when you hire.
    Whether you are a startup or an established enterprise, join our 10x faster recruitment process that reduces your hiring process by 50% and give you
    manpower consultancyefficient results.

    check our website:http://www.incruiter.com.

    ReplyDelete
  2. The Asigo System Review- Read It Before You Buy - Kingss Lyn
    The Asigo system review explains how Chris munch asigo system review has jotted down a step by step
    training to help internet marketers get fast results His.Staregies Work!

    For More Information Visit https://kingsslyn.com/the-asigo-system-review/ Here .


    ReplyDelete
  3. Nice intro good explanation thanks for sharing.
    Enrgtech Electronic Limited

    ReplyDelete