title: Figuring out from Hibernate how entity relations are configured
body: <p>Is it possible to retrieve from Hibernate (5) how the relationships between entities are configured?</p>
  
  <p>My <strong>use case</strong> is the following: I have a web service that provides (read) access to data in a relational database. The data consists of multiple entities and some of them are related through foreign keys (the usual one-to-one, one-to-many, many-to-one, many-to-many). When requesting data, client can specify a filter and, in the filter, they can freely traverse entity relationships. Something needs to translate the filter into something Hibernate understands though. Currently, we are generating <code>Criteria</code>.</p>
  
  <p>As an example, suppose we have 2 entities, <code>A</code> and <code>B</code>, where the property <code>A.bees</code> refers to a collection of <code>B</code>. This is mapped in Hibernate by having a foreign-key column in <code>B</code> referring to the primary key of <code>A</code>. A user can then request all <code>A</code>s having at least one <code>B</code> with <code>B.name</code> starting with <code>'Bla'</code>. The request specifies that we're looking for results of type <code>A</code>, and its filter part would look something like:</p>
  
  <pre><code>&lt;like&gt;
    &lt;property&gt;bees.name&lt;/property&gt;
    &lt;literal&gt;Bla*&lt;/literal&gt;
  &lt;/like&gt;
  </code></pre>
  
  <p>The <strong>problem</strong> is: my service does not control the Hibernate mapping and so does not know how the relations are configured or even which entities exist. It just loads the mapping, translates and applies the filter and spits out whatever data gets returned by Hibernate. I cannot change the request content as that is per a specification (actually OGC Filter).</p>
  
  <p>So far, I am able to find the right entities and follow the property path, encountering property <code>AssociationType</code>s and <code>EntityMetadata</code> along the way and translate all filter elements into individual <code>Criterion</code>s. What I am not able to do is to find how the entities are related: in the example, the fact that "<code>B</code> has a foreign-key referring to the primary key of <code>A</code>". Could anyone tell me how to get that information?</p>