[hibernate-dev] Extending hibernate

Sebastian smarichal at seciu.edu.uy
Tue Feb 5 12:39:39 EST 2013


Hello,

My name is Sebastián Marichal, im Computer Engineer and im from Uruguay. 
Actually im working in a great bearing system, using JavaEE and 
Hibernate as ORM.

We implemented an audit mechanism using triggers in order to know each 
insertion and deletion from the database. Everything is working very 
well, but we noticed that in many-to-many associations , when adding an 
element to a collection, hibernate deletes de full collection and 
inserts each element again. For example:

1 - We have A with a collection of B elements:  A ---> [B1,B2,B3]

2 - Add another B elemento to the collection:   A.addB(B4)

3 - Hibernate delete all the rows from the mapping table A_B and then 
inserts all the B elements again. Something like this:

         A.removeAll(B1,B2,B3)
         A.insert(B1)
         A.insert(B2)
         A.insert(B3)
         A.insert(B4)

We noticed this because the log of the audit mechanism was very dirty, 
where we expected to see "B4 inserted into A_B table" we acctually see
"B1 deleted from A_B table"
"B2 deleted from A_B table"
"B3 deleted from A_B table"
"B1 inserted into A_B table"
"B2 inserted into A_B table"
"B3 inserted into A_B table"
"B4 inserted into A_B table"


We needed to avoid this kind of dirty log, so we decided to implement 
native query for many to many collection manipulation, but this fix 
isn't good enough because the version number (we are using optimistic 
locking) of the entity isn't being updated automatically.

Is there any way to change de add and remove implementation of hibernate 
for many-to-many associations? Maybe extending hibernate? I know that 
there si an extension api but im not sure if it can be used for this 
purpose.

I would like your opinion and suggestions!


Thanks you!!

Sebastián Marichal


More information about the hibernate-dev mailing list