title: Creating a Hibernate Criteria that can filter by referenced entity
body: <p>I have an entity (PersonQuestionsEntity) that has a PersonEntity and QuestionEntity as its primary key. I use a composite key to reflect this relationship.</p>
  
  <p>Now, I want to create a Criteria object that can do the following: Find all PersonQuestion entities for a given questionId and a person's age.</p>
  
  <p>Here is my attempt at creating a Criteria for this:</p>
  
  <pre><code>    Session session = getHibernateTemplate().getSessionFactory().openSession();
      Criteria criteria = session.createCriteria(PersonQuestionsEntity.class);
      criteria.add(Restrictions.eq("question.questionId", "87"));
      criteria = criteria.createCriteria("person");
      criteria.add(Restrictions.eq("age", 23));
      criteria.setMaxResults(100);
      List l = criteria.list();
  </code></pre>
  
  <p>The problem is that I get this error: </p>
  
  <pre><code>Caused by: java.sql.SQLException: ORA-00904: "PERSONENTI1_"."AGE": invalid identifier
  </code></pre>
  
  <p>In the generated SQL, it seems the person is referenced as PERSONENTI4, not PERSONENTI1. If I copy the SQL and run it with PERSONENTIT4 instead of PERSONENTI4, it works (sort of -- it seems to be doing a cartesian join of some sort).</p>
  
  <p>Any clues as to what I might be doing wrong? I'm very new to using Hibernate.</p>
  
  <p>PersonQuestionsEntity</p>
  
  <pre><code>@Entity
  @IdClass(com.anonymous.model.PersonQuestionsKey.class)
  @Table(name = "PERSON_QUESTIONS")
  public class PersonQuestionsEntity implements Serializable
  {
      private static final long serialVersionUID = -8254277382097937813L;
  
      @Id
      @ManyToOne
      @JoinColumn(name = "USER_NAME", nullable = false)
      private PersonEntity person;
  
      @Id
      @ManyToOne
      @JoinColumn(name = "QUESTION_ID", nullable = false)
      private QuestionEntity question;
  
      @Column(name = "THEIR_ANSWER")
      private int theirAnswer;
  }
  </code></pre>
  
  <p>PersonEntity</p>
  
  <pre><code>@Entity
  @Table(name = "PERSON")
  
  public class PersonEntity implements Serializable
  {
      private static final long serialVersionUID = -1699435979266209440L;
  
      @Id
      @Column(name = "USER_NAME", length = 20, nullable = false)
      private String userName;
  
      @Column(name = "USER_NAME_REAL", length = 20, nullable = false)
      private String userNameReal;
  
      @Column(name = "AGE", nullable = false)
      private int age;
  }
  </code></pre>
  
  <p>PersonQuestionsKey</p>
  
  <pre><code>   @Embeddable
      public class PersonQuestionsKey implements Serializable
      {
          private static final long serialVersionUID = -264160855961369405L;
  
          @Id
          @ManyToOne
          @JoinColumn(name = "USER_NAME", nullable = false)
          private PersonEntity person;
  
          @Id
          @ManyToOne
          @JoinColumn(name = "QUESTION_ID", nullable = false)
          private QuestionEntity question;
  
  }
  </code></pre>