Despite my interest in all things Ruby especially Ruby on Rails, I’ve decided to have a look at some powerful enterprise Java technologies. As you know, last semester, I taught Software Architecture at Masters level and I’ve become fond of using Java once more (which I used for the lab sessions).
Of course, the fact that Java is now opensource and so are most of the most interesting Java libraries and frameworks has also influenced me.
Anyway, today I’ve spent some time with Hibernate which is a framework to map plain old Java objects (POJOs) to relational databases. Basically, it allows a programmer to write a database application using normal object-oriented development methodologies and without writing a single line of SQL.
The first step is to write a POJO. It does not have to be a Javabean (Hibernate is perfectly happy if attributes don’t have getters and setters). For instance, this will do:
public class District {
private Integer id;
private String name;
public District() {
}
}
Then write a mapping file which is basically an XML which describes how each attribute will be stored in a relational database. In many cases, this is trivial:
<hibernate-mapping>
<class name="datasource.District" table="DISTRICTS">
<id name="id" column="DISTRICT_ID" access="field">
<generator class="native" />
</id>
<property name="name" column="NAME" access="field" />
</class>
</hibernate-mapping>
In case you have a many-to-one or a many-to-many association, have a look at chapter 7 of the Hibernate documentation. It’s explicit enough.
Configuring Hibernate to connect to your database is easy (I use MySQL 5.0 with the latest Connection/J JDBC driver). Just follow the official tutorial. The important part is:
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
Finally, write a small toy application in Java to force Hibernate to create the database schema:
package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class CreateDatabaseSchema {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
session.flush();
session.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Tomorrow, I’ll use the Spring framework to decouple Hibernate from my application. I’ll surely indulge in some dependency injection. Yummy!
(Muhammad Ali picture courtesy of this website)
Raj says
While playing with Hibernate you could also have a look at iBatis http://ibatis.apache.org/
avinash says
Thanks for the info, Raj.
In fact, whenever Hibernate (or JDO) is mentioned, iBatis is also mentioned as an alternative.
I have the intention to cast a glance at iBatis in a few weeks. I’ve started with Hibernate because of its promise to allow a developer to write an enterprise application without writing a single line of SQL. I am not afraid of SQL (in fact I find SQL cool) but I want to see if that (grand) promise really works :-)
Do you use those technologies where you work?
Eddy Young says
I, myself, am a fan of JDO (the JPOX implementation, to be more specific). JDO is backed by a community-driven standard with a proper JCP/JSR, unlike Hibernate.
@Avinash: Caught a glimpse of you at Shoprite. Would have said hello, but as always, I could not recognise a face. My wife told me it was you, but then it was too late.
— Eddy
Raj says
Hi Avinash
The team I work in doesn’t use Hibernate. The main reason being that sometimes it’s easier and faster to write your own SQL queries in stored procedures etc., specially when the same database/tables are used by more than one application. When you use OOAD and design your objects first which then generate the necessary database schema then Hibernate is fine. Unfortunately, very rarely do you have the luxury of starting a database from scratch, most of the times there are things already in place and you have to add up to it and live with a previously design schema. And most important of all if you come from the very old school, like me :-), where you design your database first, make heavy use of stored procedures then the application using tools like Hibernate becomes problematic. This where IBatis comes in. They promote it as something that gives the developer full control of object mappings. To be honest I’ve only tried a couple of iBatis samples using one of it’s tutorials and I liked it because many of the things it does through a SQLMap I used to hand code them in .NET, well partially because most of the time I generate the .NET code using a mix of Excel macros (yes you can) and Query Analyser. For example in SQL Server 2000
select column_name, data_type, character_maximum_length from
information_schema.columns where table_name = ‘Person’
order by ordinal_position
gives you the column names, data type and width of a table. So, it’s very easy to write some more SQL statements to generate the necessary getters and setters for a table. I know there are many tools, free and commercial that do that for you but I’ve found it easier to use my own :-)
Raj says
Just found this
http://raibledesigns.com/rd/entry/hibernate_vs_ibatis
avinash says
To Eddy:
I used JDO and JPOX during my Software Architecture labs last semester and I was very satisfied. The integration with Eclipse (with the appropriate plugin) is great: instrumenting existing classes works really well. I’m just trying Hibernate to learn something new…
Pity we couldn’t meet. When are you both leaving? We could have lunch or dinner somewhere…
To Raj:
It’s true that most programmers have to deal with existing legacy database schemas. This is something that is acknowledged in the Hibernate documentation. As I told you, I intend to have a look at iBatis next :-)
avinash says
Something interesting I just found on the web:
Readers Choice 2004 for Persistence Framework
Of course, I’m sure one can easily find another survey with radically different results. And lots of things have changed since 2004…
Eddy Young says
@avinash: thanks, but we’re already back in england. there will be other occasions :-)
— eddy
Hasina says
Hello Avinash
Do you deliver Java Courses??
avinash says
Not for the moment. I have taught Java at undergrad and masters levels. Why are you asking?