Results 1 to 3 of 3

Thread: best way for insert update a N:M Releation

  1. #1
    Join Date
    Oct 2004
    Location
    Germany
    Posts
    143

    Default best way for insert update a N:M Releation

    Hi,

    how you are solving this Problem:

    classes:
    Code:
    Class Category {
     String name;
     List products;
    }
    
    Class Product {
     String name;
    }
    where the relation is n:m
    so you have 3 tables: category, products and category_products

    the select can be done verry simple with a join.

    inserting a product is also simple, only one insert

    but inserting a category is complex,
    i need one insert for the category and N inserts for N Category.products

    this seems to be the onyle solution, or i'm wrong ?

    but how i best update the category ??
    if my dao gets the category for Update, i can do 2 thinks:

    first:
    i delete all relations between category and product for this category and re insert the (maybe) new ones.

    second: i select all old relations from this db, compare them with the new one and create delete or insert statements for each releation.


    its long time ago i worked with rdbms and jdbc directly, hope this questions are not to bad.

    what solutions would you prefer, or what other better solution you have ?

    thank you verry much

    mfg Gideon

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Are you using JDBC or an ORM tool like Hibernate? If it's the later then this is very easy, have a look at the reference manual. If you use cascades you can save the root entity Category and the Products will be saved for you.
    http://www.hibernate.org/hib_docs/v3...kingpersistent

  3. #3
    Join Date
    Oct 2004
    Location
    Germany
    Posts
    143

    Default

    Hi,

    no i use ibatis, so this should be nearly the same as jdbc,

    yes hibernate have a cache so an update of a list can be done easy, but for my app i can't use hibernate.

    thanks mfg Gideon

Posting Permissions

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