This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Stuck on manyToMany association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Stuck on manyToMany association" Watch "Stuck on manyToMany association" New topic
Author

Stuck on manyToMany association

Noni Singh
Greenhorn

Joined: Feb 09, 2011
Posts: 20
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 (?, ?)


OCP JSE 5 Programmer, OCE JPAD, OCE WSD6
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

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]
Noni Singh
Greenhorn

Joined: Feb 09, 2011
Posts: 20
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
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1636
    
  23

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

Joined: Feb 09, 2011
Posts: 20
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
Bartender

Joined: Apr 07, 2010
Posts: 1636
    
  23

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stuck on manyToMany association
 
Similar Threads
JPA returning collection of type PersistentBag instead of ArrayList
Hibernate: association refers to an unmapped class
Many-To-One mapping in hibernate using annotations
hibernate one to many reationship.
Hibernate - Annotations - many to many association