wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes DAO create method setting object id... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "DAO create method setting object id..." Watch "DAO create method setting object id..." New topic
Author

DAO create method setting object id...

Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

Hi,

I have a question about coding style. I'm implementing the CRUD methods for my various DAOs and am considering having my create statement set the id of the object argument that it is receiving.

For example,



The DAO that includes this DAO would then use the id as a foreign key during its create method.

My functional programming instructor would cry foul at this design. I'm looking for opinions. Am I way off? Should the method return the id and then set the id in the bean?

Any thoughts are welcome.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, this is in the ORM forum however you didn't mention if you are using an ORM technology or not. So let's pretend you are using Hibernate. When you had Address to your create method and then do something like:

session.save(address)

address will then have the autognerated ID already. iBatis does the same thing. Now, if you are using plain JDBC then I don't see a problem with putting the ID in the address object in your DAO.


GenRocket - Experts at Building Test Data
Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

I am using JDBC to populate my DAO. I did consider posting my question in the JDBC forum, but assumed this one was better since I'm questioning design principles of code that moves information from a relational database to an object, which I consider ORM.

Is this forum only for discussion of ORM solutions such as Hibernate, iBatis, etc? Not trying to be cute, just need to know for future posts.

Thanks for the reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DAO create method setting object id...