This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes DAO class design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "DAO class design" Watch "DAO class design" New topic
Author

DAO class design

Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
Hello ranch members,

I have the following scenario and would like suggestions on the best possible design for it.

TableA (id_A, blah1, blah2)
TableB (id_B, id_A)

I am using MySQL and id_A and id_B are primary keys for the respective tables. I am generating values for keys using AUTO_INCREMENT in MySQL.

I have a single DAO class and want to insert data into both these tables using it.
My problem is how would I insert a value for id_A in TableB.

The only way I could think of is was that insert values in TableA and the query the newly inserted record, use this id for TableB.

There are two problems with above approach one an extra select query and second since I am auto generating the key values I do not know the value of id_A for the newly created record.

Any suggestions are appreciated.

Thanks,

Manhar.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

You can insert A, using auto generated IDs,
then retrieve the auto generated IDs using Statement.getGeneratedKeys() ,
and use that in the insert for table B.

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
Thank you Jan for your response. That does provide a solution for my design. Do you have any ideas on improving the DAO design itself?
 
jQuery in Action, 2nd edition
 
subject: DAO class design
 
Similar Threads
Hibernate mapping
CMR fights relational databases
SQL update to reverse parent-child relationship
@PrimaryKeyJoinColumns does not work
Hibernate one-to-on id mapping issue