views:

540

answers:

9

What is the simplest way to use database persistence in Java? I know, many frameworks exists around the Internet, but it could be fun to learn how to develop a persistence layer by myself and its design patterns. Where to start? Books, websites, how-tos, code-examples, etc.

Thank you.

A: 

I found this book to be particularly useful. This is a good one too.

Having created one myself I agree - it is a lot of fun, and a lot of work too. It all depends on your objectives.

Otávio Décio
+2  A: 

I would start with Sun's Java Persistence API (JPA). Here's a good starter article.

Greg
+1  A: 

At the lowest level you'll need to talk to a database so you'll be using JDBC. Since you're designed your own framework you're own your own after that. Have a look an JPA and Hibernate to get some ideas and then experiment away.

basszero
+2  A: 

The simplist way is just to use jdbc. Java has a nice tutorial here.

As far as abstraction layers go. Hibernate, in my experience, is pretty standard, and worth learning. Programming your own can be a fun exercise, but I can't think of a good reason not to use hibernate.

windfinder
I've found that using an SQL mapper like iBatis on top of JDBC is much, much easier than using raw JDBC. This should be true of ORMs like Hibernate and JPA too. In all these approaches the framework takes care of many of the details for you. These include optimizations like connection pooling and prevention of SQL injection attacks.
Jim Ferrans
+2  A: 

it could be fun to learn how to develop a persistence layer by myself

Noooo! Don't be silly.

Use JDO or JPA. The first one is a generic object persistence API, the other one is aimed at RDBMS-es only.

They have various implementations, e.g. for JPA there is EclipseLink (formerly Oracle TopLink), which is also the reference implementation for JPA 2, and Hibernate, which is also very popular.

You really, really don't want to make your own. If you want to work in this area, then contribute to one of the existing projects instead.

Jaka Jančar
+1  A: 

In addition to previous responses - check the DAO (Data Access Object) pattern - it reflects how the code for data access should be organized.

miceuz
A: 
Manoj
A: 

This book (Patterns of Enterprise Application Architecture) seems very good at first sight. I have look into it, and design patterns for developing a persistence engine are very comprehensive. It tells why, when and how to use them.

elbaid
A: 

Another library that's useful is jDBI. It takes advantage of generics to have nice binding + mapping capabilities while still being fairly close to JDBC. iBATIS is supposed to have similar characteristics though not so lightweight, and it's been around for much longer.

Chris Winters