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

  • best way for insert update a N:M Releation


    how you are solving this Problem:

    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:

    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.


    • #3

      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