Hibernate Getting Started, basic fundamentals


My expectations : My Server has some data which I need my clients to access. Now, obviously first technology I need to explore is JDBC. So I went to the sun.com site, and guess what they advice use of Hibernate. So I need to learn Hibernate. The first definition I came across is “Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer.”

This is probably not understandable for anybody learning Hibernate.

So persistence means, saving data to database. That way data is persisted, and not lost on a reboot :). Simple. And “object relational mapping” means copying of tables from database to method variables of a object. Thus making it dead easy to use the data in code.

The main reason to use Hibernate on a server platform is to get rid of the complex JDBC hell hole. The only reason you thought u needed a object relational mapping solution was to get some neatness in code and good old re-usability built into the design. So in short, for Android, people roll up your sleeves and get using some design patterns to get re-usability the right way. There is no short cut 🙂

Research to rule out

So how to get started with Hibernate. Found one fantastic blog to get started with Hibernate, now I can focus on my table design :). http://www.myeclipseide.com/documentation/quickstarts/hibernateintroduction/. But MyEclipse is a priced product :). So personally I had to skip it for now. I researched more and got Hibernator. Tried Hibernator which is a eclipse plugin to make Hibernate integration super easy (seemed too good to be true), but turned out to be a disaster. Gave it up. Also noticed that the code was as old as 2006, no one working on it now.

Finally, I hit GOLD

Then moved to Hibernate Tools from JBoss Hibernate Tools – http://download.jboss.org/jbosstools/updates/JBossTools-3.1.0.GA worked like a charm.

To get hibernate working you need to understand 3 parts of hibernate

1) hibernate.properties file : This helps you connect to your database. Select your database, server location where it is hosted, root login, password and other connection properties.

2) hibernate.hbm file : This helps you connect your tables to your objects. All you need is reference the classpath of your java class (POJO) with your table. And then connect each variable to columns. Hibernate also needs to know which column is a the primary key, and the type of each variable.

3) The hibernate library calls, to connect the database, and pass on a reference which you can use in your code to write and read from. As you want to write to the database, you need create objects and call the hibernate <b>save</b> call to issue the write transaction.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s