• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Many to Many problem

 
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I have an item class and a store class that are mapped by many to many
An item can be visible in many stores.

When I update an item hibernate goes thru all my items and inserts and delets. 4000 times since there is 4000 items.

What is the proper way of implementing many to many? I don't think I have got this right. How do I prevent the delete and insert.

Here is my annotation mapping


The store class


in the item class



How can I add an item to a store and delete an item to a store the best way without having to update 4000 queries in the database?
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mathias,

What's the code you're using for updating the item? Since you make reference both to "adding and deleting" as well as updating, I wonder if you're doing the first when you mean to do the second?

I'm not really familiar with JPA annotation as I usually use Hibernate xml mapping, but I also wonder if including the cascade mappings on the inverse side of the many-to-many is also contributing to the problem.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I want to do is simple the best way of adding a many to many relation. From a user point of view the item is added or edited. If the item is added It will be connected to one or many stores. On delete it will be delete from the store.

When adding, deleting I must load the store add the item to the store list and the store to the item list.

Store

addItem( Item item ) this.items.add( this ) item.addStore( this ) some thing like that. Is there anyone that can give me some pointers towards a healty many to many relation?
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved this using only a many to many relation on one side. Not bidirectional.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mathias,

Glad you worked it out! It might be worthwhile for you to post your updated mappings to this topic for the next person who comes along and has the same problem.
 
I will suppress my every urge. But not this shameless plug:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic