aspose file tools*
The moose likes Object Relational Mapping and the fly likes Practical way to write persistence methods / DAO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Practical way to write persistence methods / DAO" Watch "Practical way to write persistence methods / DAO" New topic
Author

Practical way to write persistence methods / DAO

William Fernandes
Greenhorn

Joined: Jul 21, 2008
Posts: 16
Hello guys!
How are you?

I am currently studying/using JDBC and I had a LOT of job with the last project (a lot of typing )

Is there any pattern that promotes ease of writing the persistence metods? I've read about DAO, but it doesn't like what I want (I guess DAO is more when you need to change your database and you won't have [many] problems).

I know that is a good thing to separate all the persistence code from the business.

Here's some code, a simple example of a bean that I want to save in a table.
Should I create another class that works with Student objects and save, select and update them?



Thanks very much.
Kathy & Bert you guys rule!


SCJP 6
"A peaceful man stands tall" — "I tremble for my country when I reflect that God is just."
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Perhaps you should look into the Object Relational forum, since it sounds like you may be interested in methods such as Hibernate and JDO. Also, keep in mind large enough systems may still require some knowledge of JDBC for certain queries.


My Blog: Down Home Country Coding with Scott Selikoff
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Is there any pattern that promotes ease of writing the persistence metods? I've read about DAO, but it doesn't like what I want (I guess DAO is more when you need to change your database and you won't have [many] problems).

Avioding typing code is not a normal driver for choosing a pattern (though I share your pain ).

DAOs are good because they seperate the persistance implementation from your application. As you have already noted they are good if you want to change databases. They are also good if you decide you don't want to use a database at all, for example if you want to run unit tests on your code with a hard coded data set. Or if you decide you want to replace JDBC with something else (such as Hibernate). It might not be immediately apparent that you will need to do this, but for the little extra effort you make you gain yourself a lot of freedom to rework your app.

If you are looking for ways to reduce the amount of code you have to personally write you might look at some of the modelling tools out there that have forward generation capabilities. With this you model your DAO, hit a button and the tool generates your code for you.
[ July 22, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Maris Orbidans
Ranch Hand

Joined: Mar 08, 2004
Posts: 149
I sometimes do something like this:



So there are 3 classes - domain object, abstract DAO and DAO implementation in JDBC
 
 
subject: Practical way to write persistence methods / DAO