Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Many to Many problem

 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • 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?
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic