Failed JPASubQuery with count() in projection (Causes parse error in OpenJPA) #1329

Closed
mats42 opened this Issue Apr 22, 2015 · 1 comment

Projects

None yet

3 participants

@mats42
mats42 commented Apr 22, 2015

I tried to get the following (stripped down) query to work:

JPAQuery query = new JPAQuery(logEntityManager);
QMessageLog messageLog = QMessageLog.messageLog;
QMessageLog otherMessageLog = new QMessageLog("otherMessageLog");

List<MessageLogProjection> messageLogResult = query.
  from(messageLog).
  where(filter).
  list(
    new QMessageLogProjection(
      messageLog, 
      new JPASubQuery().from(otherMessageLog).where(otherMessageLog.originalMessageNumber.eq(messageLog.messageNumber)).count())
    );
@Immutable
public class MessageLogProjection {
  ...
  @QueryProjection
  public MessageLogProjection(MessageLog messageLog, Long newCount) {
  ...
  }
}

but it causes an error in OpenJPA:

org.apache.openjpa.persistence.ArgumentException: An error occurred while parsing the query filter "select messageLog_, (select count(otherMessageLog) from MessageLog otherMessageLog_ where otherMessageLog_.originalMessageNumber = messageLog_.messageNumber) from MessageLog messageLog_". Error message: Attempt to query field "otherMessageLog" from non-entity variable "otherMessageLog". Perhaps you forgot to prefix the path in question with an identification variable from your FROM clause?

To me it seems that the entity name in count(otherMessageLog) is missing an underscore and should have been count(otherMessageLog_) instead or am I missing something?

If I replace count() in the sub query with eg unique(otherMessageLog.id) (which also results in a Long) the query works.

I'm new to QueryDSL and unsure about which query constructions that is supported...
Is it a bug or am I doing something wrong? Can it be worked around?

Versions used was: OpenJPA 2.3.0 and QueryDSL 3.6.3

@timowest timowest added the progress label Apr 27, 2015
@timowest timowest added this to the 3.6.4 milestone May 16, 2015
@johnktims
Member

Fixed in #1332 .

@johnktims johnktims closed this May 31, 2015
@timowest timowest removed the progress label May 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment