• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stuck on manyToMany association

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,
I'm stuck on an issue related to manyToMany association. Not sure, whether anything wrong with the mapping or is the behavior of hibernate, which is very unlikely. Any help will be appreciated.

I've 2 entities Category & Product, and a Join table "CATEGORY_PRODUCT", which has Category_id (from category table) and Product_id (from product table) columns. In my test method, trying to create association between existing entities(Category & Product), and on transaction commit, it does create association, but on console, I'm seeing hibernate is running delete query against the CATEGORY_PRODUCT table removing category records, for which I'm creating new association.

Q: Why hibernate is executing delete and how to avoid this call?



Queries from console:
Hibernate: select product0_.PRODUCT_ID as PRODUCT1_4_0_, product0_.name as name4_0_, product0_.PRODUCT_CODE as PRODUCT3_4_0_ from PRODUCT product0_ where product0_.PRODUCT_ID=?
Hibernate: select category0_.CATEGORY_ID as CATEGORY1_0_0_, category0_.DESCRIPTION as DESCRIPT2_0_0_, category0_.name as name0_0_ from CATEGORY category0_ where category0_.CATEGORY_ID=?

Hibernate: delete from CATEGORY_PRODUCT where CATEGORY_ID=?
Hibernate: insert into CATEGORY_PRODUCT (CATEGORY_ID, PRODUCT_ID) values (?, ?)
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try removing the Cascade from: @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)

PS.: On a @ManyToMany you do not need to set the fetch as Lazy, it is the default implementation. [=
 
Noni Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Removing Cascade will not avoid delete call, but will increase the number of persist call, in below scenario

Scenario: Create new Category & Product entities and save.
Without Cascade: Need to call persist on category, as well on product.


With Cascade: Need to call persist only on category, which in turn will save Product & relationship

To me delete query make sense, only when Category_id & Product_id (which are primary & foreign keys) are part of where clause.
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nonni,

Q: Why hibernate is executing delete and how to avoid this call?


I tried your example and in my setup Hibernate is only running the delete query if there is an existing relationship in de the Category_Product table between Category(1) and another Product (other than 4).

Does you Category (1) have an existing relationship before you make the update?

Regards,
Frits
 
Noni Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frits for trying the scenario.
Yes, the join table (Category_Product) has existing entries for Category (1). Firing delete before insertion is fine, but both tables primary key should be in where clause, or one has re-insert existing records while trying to add new Products relationship for same Category (1).
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Firing delete before insertion is fine, but both tables primary key should be in where clause, or one has re-insert existing records while trying to add new Products relationship for same Category (1).


Yes, you are right: when I add products to the Category(1), it deletes the existing rows in the Category_Product table and (re-)inserts the new as well as the existing rows... (this behavior is the same if I update the name of an existing product of the Category(1) and merge the it into the database)

Probably something hibernate specific, however I can't think of a logical explanation for this behavior.

Regards,
Frits
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic