Results 1 to 6 of 6
  1. #1
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default Insert values into a table with foreign key

    I'm confused on this part.
    First, I have 2 tables...1 is material table, consisting id, names, etc...
    one of the column is type id which is a foreign key from another table that gets all the material type listed and its detailed information.
    So as a material can only have one type while one type can have many materials listed in it's a one to many relation.

    Now come to my confusion when I create the codes...
    for a single table entry when I entering the type id table, it's working...
    but on the material table itself I get myself confused on how to do it, as within the fields to fill there's type id field, should that one be treated the same as the other column in one insert into statement ? or create 2 insert into one for each table ?

    Thanks in advance

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    If you are using plain JDBC then you need to run two inserts. The first insert should insert the types while the second one inserts the materials. You can't insert materials first because then there'd be no type to attach to the materials.
    These are the types of problems that persistence APIs like JPA can solve for you.

  3. #3
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    Yes, the type table will be the first to do, then the material table. Problem is when entering the material table.

    So I should use persistence API in this case ?
    I'm totally clueless on this one.
    Somehow I think running 2 inserts isn't the case here or else I'm the one don't know how to do that.
    Last edited by Levian; 07-13-2011 at 09:59 AM.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You need to insert data into two different tables with JDBC so yeah, you need two inserts.

    A persistence API provides a lot of things for you but also requires to do quite a few things upfront like reading up on JPA, writing Entity classes, deciding on an implementation and configuring it so it works properly. This might take time depending on your mileage to do at first but it will probably save you lots of time both for this project and for your future projects.

  5. #5
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    I see.

    Thanks for the reply.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    When doing the first insert you'll want to use the getGeneratedKeys method to get the id (assuming its auto generated) from the first (parent) table.
    Then this id is used for the inserts into the materials table.

    If this is still confusing you then it might be an idea to show us how you;re doing it at the moment and where it's going wrong.

Similar Threads

  1. insert the combination values
    By srikanthk035 in forum JDBC
    Replies: 2
    Last Post: 05-17-2011, 03:01 PM
  2. Not able to insert data into mysql table
    By sharanya in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 02-16-2011, 01:46 PM
  3. Replies: 1
    Last Post: 06-08-2010, 09:58 AM
  4. Replies: 0
    Last Post: 02-28-2009, 11:16 AM
  5. Replies: 0
    Last Post: 07-11-2008, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •