aspose file tools*
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 Java 8 in Action this week in the Java 8 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: 2477
    
    7

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DAO class design
 
Similar Threads
CMR fights relational databases
Hibernate one-to-on id mapping issue
@PrimaryKeyJoinColumns does not work
Hibernate mapping
SQL update to reverse parent-child relationship