org.hibernate.query.sqm.ParsingException:无法解释点标识符:品牌ID

ljsrvy3e  于 2022-11-30  发布在  其他
关注(0)|答案(2)|浏览(126)

我正在学习Hibernate,但当我尝试使用brandId进行过滤时,总是出现以下错误:

GRAVE: El Servlet.service() para el servlet [Jersey Web Application] en el contexto con ruta [/restAppJSON] lanzó la excepción [org.hibernate.query.sqm.InterpretationException: Error interpreting query [from products where brandId = 5]; this may indicate a semantic (user query) problem or a bug in the parser] con causa raíz org.hibernate.query.sqm.ParsingException: Could not interpret dot-ident : brandId at org.hibernate.query.hql.internal.BasicDotIdentifierConsumer$BaseLocalSequencePart.resolvePathPart(BasicDotIdentifierConsumer.java:225) at org.hibernate.query.hql.internal.BasicDotIdentifierConsumer.consumeIdentifier(BasicDotIdentifierConsumer.java:94) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitDotIdentifierSequence(SemanticQueryBuilder.java:2725) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGeneralPathFragment(SemanticQueryBuilder.java:2662) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGeneralPathFragment(SemanticQueryBuilder.java:149) at org.hibernate.grammars.hql.HqlParser$GeneralPathFragmentContext.accept(HqlParser.java:2947) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitPath(SemanticQueryBuilder.java:2654) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitPath(SemanticQueryBuilder.java:149) at org.hibernate.grammars.hql.HqlParser$PathContext.accept(HqlParser.java:2745) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitPathExpression(SemanticQueryBuilder.java:761) at org.hibernate.grammars.hql.HqlParser$PathExpressionContext.accept(HqlParser.java:5232) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitEqualityPredicate(SemanticQueryBuilder.java:1144) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitEqualityPredicate(SemanticQueryBuilder.java:149) at org.hibernate.grammars.hql.HqlParser$EqualityPredicateContext.accept(HqlParser.java:4310) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitWhereClause(SemanticQueryBuilder.java:1070) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitWhereClause(SemanticQueryBuilder.java:149) at org.hibernate.grammars.hql.HqlParser$WhereClauseContext.accept(HqlParser.java:4086) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpec(SemanticQueryBuilder.java:400) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelectStatement(SemanticQueryBuilder.java:262) at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitStatement(SemanticQueryBuilder.java:214) at org.hibernate.query.hql.internal.SemanticQueryBuilder.buildSemanticModel(SemanticQueryBuilder.java:161) at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:47) at org.hibernate.internal.AbstractSharedSessionContract.lambda$createQuery$1(AbstractSharedSessionContract.java:621) at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:114) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:619) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:603) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:97) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:351) at com.sun.proxy.$Proxy64.createQuery(Unknown Source) at com.edo.showroom.hibernate.DAO.ProductDAO.getProductsByBrand(ProductDAO.java:21) at com.edo.showroom.service.ProductService.getProductsByBrand(ProductService.java:13) at com.edo.showroom.resources.Products.getProductsByBrand(Products.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167) at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79) at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80) at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) at org.glassfish.jersey.internal.Errors.process(Errors.java:292) at org.glassfish.jersey.internal.Errors.process(Errors.java:274) at org.glassfish.jersey.internal.Errors.process(Errors.java:244) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680) at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394) at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

这是我在mysql的2表
创建表格brandsbrandId int(11)非空自动增量,brandName varchar(45)非空,主键(brandId),唯一键brandid_UNIQUEbrandId))引擎=InnoDB自动增量=9默认字符集=utf8mb4校对= utf8mb4_0900_ai_ci
创建表格productsproductId整数(11)非空自动增量,brandId整数(11)默认值为空,productname varchar(45)默认值为空,category varchar(45)默认空,cost双默认空,主键(productId),关键字brandid_idxbrandId)、约束brandid外键(brandId)引用brandsbrandId))引擎=InnoDB自动增量=5默认字符集=utf8mb4校对= utf8mb4_0900_ai_ci
以下是我的实体类:

@Entity(name = "products")
@Table(name = "products")
public class ProductEntity {

@Id
@Column(name = "productId")
private int productId;

@ManyToOne(targetEntity = BrandEntity.class)
@JoinColumn()
private BrandEntity brandEntity;

@Column(name = "productname")
private String productName;

@Column(name = "category")
private String category;

@Column(name = "cost")
private double cost;

public ProductEntity() {
}
  • getter和setter
@Entity(name = "brands")
@Table(name = "brands")
public class BrandEntity {
    @Id
@Column(name = "brandId")
private Integer brandId;

@Column(name = "brandName")
private String brandName;

public BrandEntity() {
}

我的DAO类是这样的:

public class ProductDAO {

SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(ProductEntity.class)
        .addAnnotatedClass(BrandEntity.class).buildSessionFactory();

public List<ProductEntity> getProductsByBrand(int brandId) {
    Session session = factory.getCurrentSession();
    session.beginTransaction();
    String stmt = "from products where brandId = " + brandId;
    List<ProductEntity> productList = session.createQuery(stmt).getResultList();
    return productList;
}

}
谁能告诉我我到底做错了什么

ql3eal8s

ql3eal8s1#

好的,我找到了问题所在,我使用的是hibernate-core 6 Alpha发行版。我切换到了5.4.12.Final,一切都在执行,没有任何问题

py49o6xq

py49o6xq2#

您正在DAO类中搜索以下内容

  • 当前代码 * 来自产品其中brandId =“+ brandId;
  • 它应该是 * 来自品牌,其中品牌ID =“+品牌ID;

这类错误属于逻辑用户训练错误一类拼写错误

相关问题