Ebean 无法识别 where 子句中的加号 (+) 运算符
Ebean doesn't recognize plus (+) operator in where clause
我正在使用 Ebean 11。32.x(适用于 Play Framework 2.7.2 的 PlayEbean 插件 5.0.1)。
我想在 where 子句中使用加号 (+) 运算符进行过滤,例如" where cost >= 50 and ((regA >=1 and regB + regC <= 0) or (regB + regC >= 1 and regA <= 0))"
。
问题是,Ebean 无法识别此运算符。如果我直接使用 MySQL 中的查询,它工作正常。抛出以下异常:
play.api.http.HttpErrorHandlerExceptions$$anon: Execution exception[[IllegalArgumentException: line 1:96 no viable alternative at input 'regB+']]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:351)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:267)
at play.core.server.AkkaHttpServer$$anonfun.applyOrElse(AkkaHttpServer.scala:448)
at play.core.server.AkkaHttpServer$$anonfun.applyOrElse(AkkaHttpServer.scala:446)
at scala.concurrent.Future.$anonfun$recoverWith(Future.scala:417)
at scala.concurrent.impl.Promise.$anonfun$transformWith(Promise.scala:41)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run(BatchingExecutor.scala:92)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
Caused by: java.lang.IllegalArgumentException: line 1:96 no viable alternative at input 'regB+'
at io.ebeaninternal.server.grammer.EqlParser$ErrorListener.syntaxError(EqlParser.java:45)
at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:544)
at org.antlr.v4.runtime.DefaultErrorStrategy.reportNoViableAlternative(DefaultErrorStrategy.java:310)
at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:136)
at io.ebeaninternal.server.grammer.antlr.EQLParser.any_expression(EQLParser.java:1744)
at io.ebeaninternal.server.grammer.antlr.EQLParser.conditional_primary(EQLParser.java:1577)
at io.ebeaninternal.server.grammer.antlr.EQLParser.conditional_factor(EQLParser.java:1531)
at io.ebeaninternal.server.grammer.antlr.EQLParser.conditional_term(EQLParser.java:1475)
at io.ebeaninternal.server.grammer.antlr.EQLParser.conditional_expression(EQLParser.java:1406)
有什么方法可以实现吗?
使用 filter()
方法测试 Ebean 实体:
package models.entities;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import io.ebean.Model;
import io.ebean.Query;
@Entity
@Table(name="test")
public class Test extends Model {
@Column(nullable=false, name="cost")
public int cost;
@Column(nullable=false, name="regA")
public int regA;
@Column(nullable=false, name="regB")
public int regB;
@Column(nullable=false, name="regC")
public int regC;
public static List<Test> filter(){
EbeanServer ibs = Ebean.getServer("ibs");
Query<Test> query = ibs.createQuery(Test.class, " where cost >= 50 and ((regA >=1 and regB + regC <= 0) or (regB + regC >= 2 and regA <= 0))");
return query.findList();
}
}
Ebean 的主要维护者 Rob Bygrave 在 https://groups.google.com/forum/#!forum/ebean 上提出了以下解决方案,效果很好:
Query<Test> query = ibs.createQuery(Test.class)
.where()
.raw("cost >= 50 and ((regA >=1 and regB + regC <= 0) or (regB + regC >= 2 and regA <= 0))")
.query();
我正在使用 Ebean 11。32.x(适用于 Play Framework 2.7.2 的 PlayEbean 插件 5.0.1)。
我想在 where 子句中使用加号 (+) 运算符进行过滤,例如" where cost >= 50 and ((regA >=1 and regB + regC <= 0) or (regB + regC >= 1 and regA <= 0))"
。
问题是,Ebean 无法识别此运算符。如果我直接使用 MySQL 中的查询,它工作正常。抛出以下异常:
play.api.http.HttpErrorHandlerExceptions$$anon: Execution exception[[IllegalArgumentException: line 1:96 no viable alternative at input 'regB+']]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:351)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:267)
at play.core.server.AkkaHttpServer$$anonfun.applyOrElse(AkkaHttpServer.scala:448)
at play.core.server.AkkaHttpServer$$anonfun.applyOrElse(AkkaHttpServer.scala:446)
at scala.concurrent.Future.$anonfun$recoverWith(Future.scala:417)
at scala.concurrent.impl.Promise.$anonfun$transformWith(Promise.scala:41)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run(BatchingExecutor.scala:92)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
Caused by: java.lang.IllegalArgumentException: line 1:96 no viable alternative at input 'regB+'
at io.ebeaninternal.server.grammer.EqlParser$ErrorListener.syntaxError(EqlParser.java:45)
at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:544)
at org.antlr.v4.runtime.DefaultErrorStrategy.reportNoViableAlternative(DefaultErrorStrategy.java:310)
at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:136)
at io.ebeaninternal.server.grammer.antlr.EQLParser.any_expression(EQLParser.java:1744)
at io.ebeaninternal.server.grammer.antlr.EQLParser.conditional_primary(EQLParser.java:1577)
at io.ebeaninternal.server.grammer.antlr.EQLParser.conditional_factor(EQLParser.java:1531)
at io.ebeaninternal.server.grammer.antlr.EQLParser.conditional_term(EQLParser.java:1475)
at io.ebeaninternal.server.grammer.antlr.EQLParser.conditional_expression(EQLParser.java:1406)
有什么方法可以实现吗?
使用 filter()
方法测试 Ebean 实体:
package models.entities;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import io.ebean.Model;
import io.ebean.Query;
@Entity
@Table(name="test")
public class Test extends Model {
@Column(nullable=false, name="cost")
public int cost;
@Column(nullable=false, name="regA")
public int regA;
@Column(nullable=false, name="regB")
public int regB;
@Column(nullable=false, name="regC")
public int regC;
public static List<Test> filter(){
EbeanServer ibs = Ebean.getServer("ibs");
Query<Test> query = ibs.createQuery(Test.class, " where cost >= 50 and ((regA >=1 and regB + regC <= 0) or (regB + regC >= 2 and regA <= 0))");
return query.findList();
}
}
Ebean 的主要维护者 Rob Bygrave 在 https://groups.google.com/forum/#!forum/ebean 上提出了以下解决方案,效果很好:
Query<Test> query = ibs.createQuery(Test.class)
.where()
.raw("cost >= 50 and ((regA >=1 and regB + regC <= 0) or (regB + regC >= 2 and regA <= 0))")
.query();