Archive for February 2008

Development diary #1

I thought that I’d be sharing some of my experiences developing my Java based blog now that I have progressed slightly.

First I have to tell you about Hibernate it is just so wonderful tool. In short it is a framework for persisting data to database. In long, it really shaves off some of the less delightful aspects of database coding. Depending on your database structure you don’t have to write single statement or either have to but much lesseraspect than in purely JDBC-way. I had some initial trouble to get it up and running but that was my own hindsight in creating proper database structure. That was not too big surprise taking in to mind that I were really green on Java and sql when I initially conceived this blog project. Experience comes with time so to say..
After producing consistent database structure with foreign keys and indexes and what-not. I got my Hibernate mapping working and it was time to code some DAO layer to project. Now I have proper DB layer and DAO schema implemented, this means that I can now easily query stuff from database from business logic layer by only calling DAO’s with something like:

Session session = null;
Transaction tx = null;
PostsDAO postDAO = null;
try{
postDAO = new PostsDAO();
session = postDAO.getSession();
tx = session.beginTransaction();
Post post = postDAO.findWithID(postID); // get post with postID
//or something like
List posts = postDAO.findWithinMonth(date) // where date on java.util.Date, gets posts that were published on same month
//or like
List posts = postDAO.findWithDate(date) //get posts that were published on exact date

Then I can just for example add the post or list to Frontpage class that contains values like

- Sidebar, contains sidebar menu items
- Posts, contains posts
- Footer, contains dynamic footer objects
- Header, contains dynamic header objects

Then I’ll just set frontpage as request parameter and get the stuff on on the .jsp using jstl.
Nice, clean and structured. Also when inserting rows to database Hibernate comes very handy, for example if you have some tables like:

-Post
-Category2Post <- this being relational table to map many-to-many relationships
-Category

And on code side you have couple hibernate pojos portraying those db tables, namely, posts, category2post and category. Hibernate can automagically insert stuff to middle table if you do something like this:
When inserting new post you have fetched post related stuff from form or struts action form or etc..
So you have Post post = new Post();
post.setTitle(title_of_the_post); or smtn..
.
.
.
Then you usually have some Map categories2post map on your post bean, as in this case our post supports multiple categories.
So then you just create new Category2post
Fetch category by categoryID from db and category2post.setCategory(Category)
category2post.setPost(post);
and post.addCategories(category2post);
after this you can save this bean using postDAO
postDAO.saveOrUpdate(post);

Read the rest of this entry »