2016-05-12 3 views
1

Ich versuche, eine Abfrage zu erstellen, wo ich Rundung zu einem ausgewählten Feld hinzufügen muss (Einstellung Dezimalstelle 1).Inkonsistenz mit generierten SQL-Abfrage bei Verwendung von MathExpressions.round

Dies ist mein Code:

JPAQuery query = new JPAQuery(em);    
QMyEntity myEntity = QMyEntity.myEntity;   
QSecondaryEntity secondaryEntity = QSecondaryEntity.secondaryEntity;  

Expression<String> idExpression= (new CaseBuilder() 
      .when(myEntity.type.eq(4)).then(myEntity.normalId) 
      .otherwise(myEntity.specialId)).as("id"); 

NumberExpression<BigDecimal> roundExpression = MathExpressions.round(myEntity.qty, 1).as("roundedQty"); 

query = query.from(browse, lips).where(predicate); 

return query.list(Projections.constructor(MyEntityDto.class, myEntity.field1, myEntity.field2, roundExpression, idExpression)); 

MyEntity nicht direkt eine Datenbanktabelle Mapping "MyEntity" genannt, sondern "Table0".

Wenn ich die erzeugte Abfrage sehe ich sehen:

select myEntity.field1, myEntity.field2, round(myEntity.qty), 
     (case when (myEntity.type = ?1) then myEntity.normalId 
             else myEntity.specialId end) as id 
from MyEntity myEntity 
where myEntity.id = ?2 and ... 

Und ich bekomme diese Fehlermeldung:

The SELECT clause has 'round' and '(myEntity.qty)' that are not separated by a comma.

Während, wenn ich den runden Ausdruck entfernen die erzeugte Abfrage ok ist und die richtige Tabelle abfragt in der Datenbank:

SELECT t1.Field1 AS a1, t0.Field2 AS a2, t0.Quantity AS a3, 
     CASE WHEN (t0.Type = ?) THEN t3.ID ELSE t3.SecondID END 
FROM Table7 t7, Table t6,..., Table1 t1, Table0 t0 
WHERE ((((((((t ... 

Ich verwende die MathExpression korrekt? Was ist die richtige Art, ein Feld in der Select-Klausel zu runden?

Antwort

0

Ich denke, es ist ein Bibliothekswanze. Wenn Sie versuchen, ansonsten die gleiche Abfrage zu tun ‚runde‘ setzen mit Zitaten wird es scheitern auch:

Expression round = TemplateExpressionImpl.create(BigDecimal.class, "'round(myEntity.qty, 1)' as roundedQty"); 

oder

Expression round2 = TemplateExpressionImpl.create(BigDecimal.class, "'round({0}, {1})' as roundedQty", myEntity.qty, 1); 

könnten Sie versuchen, Create, aber Sie müssen die vollständige Abfrage angeben :

Query q = em.createQuery("SELECT round(myEntity.qty, 1) FROM MyEntity myEntity WHERE ...."); 

es ist auch nicht, aber mit Create können Sie die Anführungszeichen und es wird funktionieren:

Query q = em.createQuery("SELECT 'round(myEntity.qty, 1)' FROM MyEntity myEntity WHERE ...."); 
Verwandte Themen