title: join query on one-to-one Grails domain using GORM with parameters
body: <p>I have a two Grails <code>domain</code> class, let say:</p>
  
  <pre><code>class Hero {
      String name
      Float level
  
      Familiar familiar
  }
  
  class Familiar {
      String name
      Integer raceId
  }
  </code></pre>
  
  <p>Now, what I want is to retrieve all <code>Hero</code> having <code>Familiar</code> with the name similar to a given <code>String</code>, for <em>example</em>: <code>"hachiko"</code>. To do that using <code>SQL</code>, all I need is to perform a query similar to this:</p>
  
  <pre><code>SELECT h.name, h.level
  FROM HERO h JOIN FAMILIAR f ON h.id = f.ownerId
  WHERE f.name LIKE '%hachiko%'
  </code></pre>
  
  <p>But how can I do that on <code>Grails</code>? This is my code:</p>
  
  <pre><code>String filter = "hachiko"
  def hero = Hero.executeQuery(
      "SELECT h.name, h.level FROM HERO h JOIN h.familiar WHERE h.id = " +
      "h.familiar.ownerId and h.familiar.name LIKE :filter",
      [filter, "%$filter%"]
  )
  </code></pre>
  
  <p>Would this work? Should I create those <code>static</code> properties (<code>hasOne</code>, <code>belongsTo</code>), and where should I place them? Because so far, without those <code>static</code> properties, I'm receiving an <code>error</code> message:</p>
  
  <blockquote>
    <p><code>ORA-00904: "HERO0_"."FAMILIAR_ID": invalid identifier</code></p>
  </blockquote>
  
  <p>And having those <code>static</code> properties added, I got this:</p>
  
  <blockquote>
    <p><code>org.hibernate.MappingException: hasOne property [Hero.familiar] is not bidirectional. Specify the other side of the relationship!</code></p>
  </blockquote>
  
  <p>Their relationship is <em>optional</em> one-to-one. (e.g. A <code>Hero</code> can have at most one <code>Familiar</code>), and the <code>id</code> of the <code>Familiar</code> is the same of it's <code>Hero</code>.</p>