This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Hibernate with strange behavior

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some tests with Hibernate, to remove an entity that appears in 3 lines of an intermediate table, instead of removing it to 3 lines in this entity, he removed and inserted again ALL lines referring to those who remained in the intermediate table.

To understand better, see the code:

Entity TEAM:Entity TEAMTYPE:Entity INDIVIDUAL:Hibernate generated the following SQL script:The database was empty, so TeamTypes, Teams and individuals were added:In order to obtain such a situation that attend this diagram:



Then, the intermediate table was something like this:The problem I found is to remove the individual P7 with ID 7. Here he is on 3 teams, and correctly displayed on 3 lines of intermediate table with the correct relationship with the 3 teams.

The code to remove it is this:In implementing this, Hibernate should do everything I need, besides deleting the row relating to the individual, it should only delete 3 lines in the intermediate table. Except that instead he:I have also implemented a trigger in the database (Oracle) and it inserted as well as deleted. I suspected that the INSERTS were informational only. It does delete individual correctly. But it's inserting again into the intermediate table. The most strange is that after this operation the intermediate table is correct and no exceptions are thrown.

I can't understand why Hibernate is doing this.

For a table with 12 records, this is "peanuts", but imagine if I had 2,000,000 and remove 3? it would reinsert 1,999,997?

Where am I missing?

Regards from Brazil!
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marcus Laranjeira wrote:



not sure whether you need to remove the individual from all the teams....
I have not worked much on many to many mappings in hibernate, but i am wondering why a simple "session.remove(p7)" will not work.
Can you try it with "session.remove(P7)" and see if any exception comes...

To Brazil, with love...
 
Marcus Laranjeira
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rahul,

Thanks for your reply!

I did it already! Look at the exceptions:





Thanks again!
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can try to put the cascade="all" attribute for your association from Individual to the Team ...
 
Marcus Laranjeira
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul,



I tried again with your suggestion and it did not work. Remains exactly the same problem.

Tested the various CASCADE options and there was no change in behavior.

 
Marcus Laranjeira
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved!

I used SET instead of LIST!

Look at here and here!!!

Now it works!
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing the solution and the links...
 
brevity is the soul of wit - shakepeare. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic