I was trying to do this for log entries. I wanted one LOG table, but also to maintian a reference from the log entry to the entity that was modified. The problem is basically that you need to include the table name as part of the database 'address'. This is not something SQL does easily, however Hibernate can help.
I did it by creating a class called LogEntry (in your case Role), which specified the table name and everything. Then made a subclass for each entity I wanted to log (only a few), call it FooLogEntry (in yourcase GroupRole and UserRole), which I ONLY used to define a descriminator column, no extra fields. This way when Hibernate goes to populate the set of LogEntrys associated with Foo, it actually uses the FooLogEntry class. Of course you have to have Foo build the LogEntry for you, as only it should know about FooLogEntry. This seems to work nicely, although it is a bit complex.
Once set up, it allows me to have one-to-may collections, where the 'many' table is pointed to from multiple 'one' tables. I can go into more (better?) detail, if this sounds like what you're looking for, or if I sound crazy because somebody has a better solution. =)
--Ryan
|