Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple relationships with single mapping table without generating foreign keys by Hibernate
    primarykey
    data
    text
    <p>I have <strong>two base abstract classes</strong> and there are multiple additional classes derived from these two, adding additional attributes etc.</p> <p>There exist relations between those specific derived types.</p> <p><strong>A simple illustrating example:</strong></p> <p><code>Role</code> and <code>Group</code> classes are <code>abstract</code>, but they are not marked as <code>@MappedSuperclass</code>. </p> <p><code>InheritanceType.JOINED</code> strategy is being used, so both tables <code>Role</code> (for abstract class) and <code>AdminRole</code> (for derived class) should exist (they both will have the same <code>RoleID</code>).</p> <ul> <li><code>DiscussionGroup</code> has one <code>AdminRole</code> property, a <code>Set&lt;DiscussantRole&gt;</code>, a <code>Set&lt;ManagerRole&gt;</code> </li> <li><code>WorkingGroup</code> has <code>Set&lt;WorkerRole&gt;</code>, <code>Set&lt;ManagerRole&gt;</code></li> </ul> <blockquote> <pre><code>Role |-- AdminRole |-- DiscussantRole |-- ManagerRole |-- WorkerRole Group |-- DiscussionGroup |-- WorkingGroup </code></pre> </blockquote> <p>Because the number of derived classes can grow, and because classes derived from Role can have relations to different classes derived from Group (and vice versa) this would lead to a large amount of different mapping tables (Worker_DiscussionGroup, Worker_WorkingGroup) or multiple foreign key columns (in M:1 relationships - e.g. a ManagerRole would have to have DiscussionGroupID and WorkingGroupId). <strong>I want to map all these relations through one common mapping table.</strong></p> <pre><code>Role_Group (RoleID, GroupId) </code></pre> <p>We use <strong>Hibernate to generate the DDL schema (hbm2ddl.auto=create)</strong> during current development (we will use a static schema definition for later production use). Hibernate automatically creates Foreign keys for the relations, and thats quite good for us. </p> <p>If I instruct it to use the same mapping a table for joins (for the many-to-many, many-to-many and for one-to-one as well), <strong>it will try to create foreign keys</strong> as well. And it is of course not possible to create a foreign key on <code>RoleID</code> from <code>Role_Group</code> to <code>AdminRole</code> and <code>DiscussantRole</code> at the same time, so I get an error.</p> <p><strong>Is there any way, how to instruct Hibernate</strong></p> <ol> <li><p><strong>to generate selected relationships without foreign keys</strong></p> <p>or </p></li> <li><p><strong>to define that the relation should be based on the abstract ancestors (i.e. DiscussionGroup and its Set should be mapped as 1:N - Group and Set)?</strong></p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload