title: Casting in JPA specification
body: <p>I have this specification:</p>
  
  <pre><code>public static Specification&lt;CompensationCase&gt; containsClaimantName(final String firstName, final String middleName, final String surName, final String motherMaidenName) {
      return new Specification&lt;CompensationCase&gt;() {
          public Predicate toPredicate(Root&lt;CompensationCase&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
              Path&lt;Claimant&gt; claimantPath = root.get(CompensationCase_.claimant);
  
              Subquery&lt;ClaimantPerson&gt; claimantPersonSubquery = query.subquery(ClaimantPerson.class);
              Root&lt;ClaimantPerson&gt; claimantPersonRoot = claimantPersonSubquery.from(ClaimantPerson.class);
  
              claimantPersonSubquery.
                      select(claimantPersonRoot).
                      where(
                              ClaimantPersonSpecs
                                      .containsPersonName(firstName, middleName, surName, motherMaidenName)
                                      .toPredicate(claimantPersonRoot, query, cb));
  
              return cb.in(claimantPath.as(ClaimantPerson.class)).value(claimantPersonSubquery);
          }
      };
  }
  </code></pre>
  
  <p>Where <code>ClaimantPerson</code> inherits from <code>Claimant</code>. I read you could cast using <code>.as()</code> but I'm unsure if I'm doing it right. Currently I'm getting this error:</p>
  
  <pre><code>--Erroring: batchId[2] message[java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '.' near line 1, column 137 [select generatedAlias0 from test.project.compensation.model.CompensationCase as generatedAlias0 where cast(generatedAlias0.claimant as test.project.compensation.model.ClaimantPerson) in (select generatedAlias1 from test.project.compensation.model.ClaimantPerson as generatedAlias1 where generatedAlias1.personRef in (select generatedAlias2 from test.project.entity.identity.PersonRef as generatedAlias2 where ( ( ( lower(generatedAlias2.name.firstName) like :param0 ) and ( 1=1 ) ) and ( lower(generatedAlias2.name.surname) like :param1 ) ) and ( lower(generatedAlias2.name.motherMaidenName) like :param2 )))]] 
  </code></pre>
  
  <p>(note that column 137 refers to test.project.compensation.model.ClaimantPerson)</p>
  
  <p>I'm sure it has something to do with the casting. How could I reference <code>ClaimantPerson</code> when I have a property <code>Claimant</code> under <code>CompensationCase</code>?</p>
  
  <p>Here are snippets of class/entity definitions:</p>
  
  <p>CompensationCase is the class I'm querying</p>
  
  <pre><code>@Entity
  @Table(name = "project_compensation_case")
  @EntityListeners({CompensationCaseNumberListener.class})
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class CompensationCase extends AutoIdBasedEntity{
  
       @Valid
       @NotNull(message = "{compensationCase.claimant.NotNull}")
       @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = false)
       @ForeignKey(name = "FK_CompensationCase_Claimant")
       @JoinColumn(name = "claimant_id", unique = true, updatable = false, nullable = false)
       private Claimant claimant;
       //So on....
  }
  </code></pre>
  
  <p>This is claimant:</p>
  
  <pre><code>@Entity
  @Table(name = "project_compensation_claimant")
  @Inheritance(strategy = InheritanceType.JOINED)
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
      include = JsonTypeInfo.As.PROPERTY,
      property = "objectType")
  @JsonSubTypes({
      @JsonSubTypes.Type(ClaimantPerson.class),
      @JsonSubTypes.Type(ClaimantOrganization.class)
  })
  public abstract class Claimant extends AutoIdBasedEntity{
       //stuff here
  }
  </code></pre>
  
  <p>This is claimantPerson:</p>
  
  <pre><code>@Entity
  @Table(name = "project_compensation_claimant_person")
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
      include = JsonTypeInfo.As.PROPERTY,
      property = "objectType")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class ClaimantPerson extends Claimant {
      //properties and stuff
  }
  </code></pre>
  
  <p>Appreciate any help. Thanks!</p>