Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DAO class design

 
Manhar Puri
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2602
13
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manhar Puri
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic