Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate with strange behavior

 
Marcus Laranjeira
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!
 
Rahul Babbar
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic