Using multiple secondaryTable on same table in a class hierarchy


I’ve been having trouble with SecondaryTables recently.
This problem is similar to Hibernate @SecondaryTable() for both Parent and Child with the same name

I have the following data model :

@Inheritance(strategy = SINGLE_TABLE)
public class A { [...] }

@SecondaryTable(name = "B")
public class B extends A { [...] }

@SecondaryTable(name = "B")
public class C extends B { [...] }

When two (or more) Secondary Tables refere to the same table in a hierarchy, Hibernate will insert for each entity, instead of inserting once and then updating the table. This causes a unique constraint violation on the data base.

I know the simple answer to this is to change the model for a more hibernate friendly model.
For some context, I’m working on migrating an old management application to hibernate, thus cannot change the model that easily.

For now, the fix I’ve found is to create an hibernate Interceptor that looks for entities mapped with @SecondaryTable, and change the SqlInsert with an update statement if a Secondary Table is already declared on the same table in the hierarchy.

Do you plan on handling this kind of secondary table mapping in future versions of hibernate ?

For info Clément works in the same company as me, Efluid, and on the subject of hibernate migration.

Clément can you put the code of the interceptor here?

Here is the integrator that I came up with to fix this issue.
It simply replaces the sql insert statements by update statements when the problem occurs (i.e. if a parent entity has the same SecondaryTable)