title: How to query on inherited classes with querydsl
body: <p>I use querydsl to query on a mongodb. As allowed by mongodb, in several cases I store objects of different types in the same collection.</p>
  
  <p>For instance, in my data model I have:</p>
  
  <pre><code>interface Notification {
      NotificationType getType(); // EMAIL, SMS etc.
  }
  
  interface EmailNotification extends Notification {
      Set&lt;User&gt; getRecipients();
  }
  </code></pre>
  
  <p>Now I want to query for Notifications of any kind (not only EmailNotification), but in the case I have EmailNotifications, I want to filter on some recipient.</p>
  
  <p>I tried this code (doesn't work):</p>
  
  <pre><code>final QNotification notification = QNotification.notification;
  final BooleanBuilder typesPredicate = new BooleanBuilder();
  // "recipientEmails" and "recipientPhones" are provided parameters (lists of String)
  typesPredicate.or(notification.type.eq(NotificationType.EMAIL)
                     .and(notification.as(QEmailNotification.class).recipients
                          .any().email.in(recipientEmails)));
  
  typesPredicate.or(notification.type.eq(NotificationType.SMS)
                     .and(notification.as(QSMSNotification.class).recipients
                          .any().phoneNumber.in(recipientPhones)));
  
  
  notificationPersister.query(Notification.class).where(typesPredicate);
  </code></pre>
  
  <p>It doesn't throw any error or exception, but I don't get the expected result (actually I don't get any result) because the generated mongo query is wrong, and I can't get how to make it right.</p>
  
  <p>Generated query is like:</p>
  
  <pre><code>{
     "$and":[  
        // ...
        {  
       "$or":[
          {  
             "type":"EMAIL",
             "notification.recipients.email":{  
                "$in":[  
                   "a@b.com"
                ]
             }
          },
              // ...
       ]
        }
     ]
  }
  </code></pre>
  
  <p>And the issue lies in the key "notification.recipients.email": it should be just "recipients.email".</p>
  
  <p>Why does "notification.as(QEmailNotification.class).recipients" translates to "notification.recipients", and how can I make it as expected ? </p>
  
  <p>Note that the following would work:</p>
  
  <pre><code>notificationPersister.query(EmailNotification.class).where(
      QEmailNotification.eMailNotification.recipients.any().email.in(recipientEmails));
  </code></pre>
  
  <p>But then I'm forced to run 1 query per inherited class, which is not efficient.</p>