Announcement Announcement Module
Collapse
No announcement yet.
best way for insert update a N:M Releation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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

      Comment

      Working...
      X