File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate with strange behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate with strange behavior" Watch "Hibernate with strange behavior" New topic
Author

Hibernate with strange behavior

Marcus Laranjeira
Greenhorn

Joined: Jul 31, 2009
Posts: 4
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

Joined: Jun 28, 2008
Posts: 210
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...

Rahul Babbar
Marcus Laranjeira
Greenhorn

Joined: Jul 31, 2009
Posts: 4
Hello Rahul,

Thanks for your reply!

I did it already! Look at the exceptions:





Thanks again!
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
I think you can try to put the cascade="all" attribute for your association from Individual to the Team ...
Marcus Laranjeira
Greenhorn

Joined: Jul 31, 2009
Posts: 4
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

Joined: Jul 31, 2009
Posts: 4
Problem solved!

I used SET instead of LIST!

Look at here and here!!!

Now it works!
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Thanks for sharing the solution and the links...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate with strange behavior