Mapping a "legacy" polymorphic join

We have a database structure dictated by Hibernate/Java. The java version is used for runtime and Ruby/Rails for administration/integration etc…

We have a structure where a Channel holds a ChannelBehaviour (in java). The ChannelBehaviour is subclassed by several different classes. Every class has it’s own table and each table holds a reference back to the Channel class (Channel table itself doesn’t hold a reference to the behaviour although the objects does).

Since Ruby has duck typing I don’t have to inherit a ChannelBehaviour for each behaviour in Rails. I was thinking of having a module instead that each behaviour class needs to include. This could ensure that the behaviour class is added to Channel to be used when generating SQL statements to find the referenced behaviours from Channel (using “union” or something like that). Anyone has any ideas how this best would be done?

Another problem I have is that Hibernate requires all ids in the various behaviour tables to be unique over all of the tables (ie a MulticastChannelBehaviour and a DvbtChannelBehaviour cannot have the same id despite living in different tables). In java this is handled transparently by Hibernate where Hibernate uses a sequence table in the database to generate ids. Because of this I have to set ids manually in the behaviour classes (using the same sequence table). I have tried and failed. How should this be done?

Answers and ideas for any of this is most welcome :slight_smile: