| Author |
Stuck on manyToMany association
|
Nonni Singh
Greenhorn
Joined: Feb 09, 2011
Posts: 7
|
|
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 (?, ?)
|
 |
Hebert Coelho
Ranch Hand
Joined: Jul 14, 2010
Posts: 718
|
|
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. [=
|
[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
|
 |
Nonni Singh
Greenhorn
Joined: Feb 09, 2011
Posts: 7
|
|
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.
|
 |
Frits Walraven
Rancher
Joined: Apr 07, 2010
Posts: 742
|
|
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
|
 |
Nonni Singh
Greenhorn
Joined: Feb 09, 2011
Posts: 7
|
|
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
Rancher
Joined: Apr 07, 2010
Posts: 742
|
|
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
|
 |
 |
|
|
subject: Stuck on manyToMany association
|
|
|