title: Hibernate: How to map @ManyToOne with polymorphism (@ManyToAny)
body: <p>Say I have at least two entities.</p>
  <pre><code>@Entity
  public class Process {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(unique = true)
      private String name;
  
      @ManyToAny(
              metaColumn = @Column(name = &quot;node_type&quot;),
              fetch = FetchType.LAZY
      )
      @AnyMetaDef(
              idType = &quot;long&quot;, metaType = &quot;string&quot;,
              metaValues = {
                      @MetaValue(targetEntity = Milestone.class, value = MILESTONE_DISC),
                      @MetaValue(targetEntity = Phase.class, value = PHASE_DISC)
              }
      )
      @Cascade({org.hibernate.annotations.CascadeType.ALL})
      @JoinTable(
              name = &quot;process_nodes&quot;,
              joinColumns = @JoinColumn(name = &quot;process_id&quot;, nullable = false),
              inverseJoinColumns = @JoinColumn(name = &quot;node_id&quot;, nullable = false)
      )
      private Collection&lt;ProcessNode&gt; nodes = new ArrayList&lt;&gt;();
  
      ...
  }
  </code></pre>
  <hr />
  <pre><code>@Entity
  @ToString
  @DiscriminatorValue(MILESTONE_DISC)
  public class Milestone implements ProcessNode {
  
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private String name;
  
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private Collection&lt;ResultDefinition&gt; results;
  
      @ManyToOne()
      private Process process;
  
  ...
  }
  </code></pre>
  <p>When I try to create a Process the following is executed:</p>
  <blockquote>
  <p>insert into milestone (id, name, process) values (null, ?, ?)</p>
  </blockquote>
  <p>How do I map the @ManyToOne property, that the the process is not inserted into the milestone table but is obtained by joining with the process_nodes table?</p>
  <hr />
  <p><strong>Edit:</strong></p>
  <p>I now did the following:</p>
  <pre><code>   @ManyToOne(fetch = FetchType.LAZY)
      @Transient
      private Process process;
  </code></pre>
  <p>I can access the process from the milestone and the process is not saved in the milestone table.</p>
  <p><strong>Is there a cleaner way to achieve that?</strong></p>
  <hr />
  <p><strong>second edit</strong></p>
  <p>More details:</p>
  <p>ProcessNode is an interface where two other entities inherit from. With hibernate annotations @ManyToAny I got it to map it into the following structure:</p>
  <p><a href="https://i.stack.imgur.com/aPvji.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aPvji.png" alt="my table structure" /></a></p>