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?
- Hibernate tooling and project setup
- Establishing connection between Java application and RDBMS using Hibernate Configuration (hibernate.cfg.xml), SessionFactory and Session
- Different Hibernate mapping options between Java Entities and RDBMS tables
- Load, Get or Query records of database in the form of Java objects by using Hibernate
- Perform Save, Update, Delete operations in database using Hibernate Transactions
- Learn to use Hibernate annotations instead of xml configuration
- Learn advance topics in Hibernate - caching, batch processing…
- Leverage code further using Spring framework and Hibernate integration
How to get started with Hibernate learning?
- Download sample code snippet for hibernate learning made easy, which is available @ https://github.com/tirthalpatel/Learning-Hibernate
- Follow instructions of "README.md" file for project setup.
- Now run and explore code of - Hello World tutorial.
- 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
- ABC of Hibernate
- Explore Hibernate documentation
- Install Jboss Tools Plugins in Eclipse
- Hibernate Tutorials at - tutorialspoint & javatpoint
