Hibernate QuerySyntaxException:未映射

柠檬汁

我正在使用Jersey开发REST API Hibernate数据库是MySQL我有几个层在我的设计中,dao layerservice layerREST layer请检查以下代码。

NotificationJSONService.java(REST层)

@GET
    @Path("/getActiveNotifications")
    @Produces (MediaType.APPLICATION_JSON)
    public List<Notification> getActiveNotifications()
    {
        NotificationService service = new NotificationService();
        List<Notification> notificationsByUser = service.getActiveNotifications();
        return notificationsByUser;
    }

NotificationService.java(服务层)

public List<Notification>getActiveNotifications()
    {
        Session session = getSession();
        Transaction transaction = null;
        List<Notification> list = new ArrayList<Notification>();

        try
        {
            transaction = getTransaction(session);
            list = notificationInterface.getActiveNotifications(session);
            transaction.commit();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            session.close();

            for (int i = 0; i < list.size(); i++) {
                User user = new User();
                user.setIduser(list.get(i).getUser().getIduser());
                list.get(i).setUser(user);

                Product product = new Product();
                product.setIdproduct(list.get(i).getProduct().getIdproduct());
                list.get(i).setProduct(product);

                NotificationType notificationType = new NotificationType();
                notificationType.setIdnotificationType(list.get(i).getNotificationType().getIdnotificationType());
                list.get(i).setNotificationType(notificationType);
            }
        }

        return list;
    }

NotificationDAOImpl.java (DAO Layer)

@Override
    public List<Notification> getActiveNotifications(Session session) 
    {
        Query query = session.createQuery("from notification where delete_timestamp IS NULL");
        List list = query.list();
        return list;
    }

如您所见,以上所有代码都与名为的表相关notification以下是notificationbean信息。

Notification.java

public class Notification  implements java.io.Serializable {


     private Integer idnotification;
     private NotificationType notificationType;
     private Product product;
     private User user;
     private Date deleteTimestamp;
     private Date dateCreated;
     private Date lastUpdated;

    public Notification() {
    }

    public Integer getIdnotification() {
        return this.idnotification;
    }

    public void setIdnotification(Integer idnotification) {
        this.idnotification = idnotification;
    }
    public NotificationType getNotificationType() {
        return this.notificationType;
    }

    public void setNotificationType(NotificationType notificationType) {
        this.notificationType = notificationType;
    }
    public Product getProduct() {
        return this.product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    public Date getDeleteTimestamp() {
        return this.deleteTimestamp;
    }

    public void setDeleteTimestamp(Date deleteTimestamp) {
        this.deleteTimestamp = deleteTimestamp;
    }
    public Date getDateCreated() {
        return this.dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    public Date getLastUpdated() {
        return this.lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }
}

Notification.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 31, 2019, 6:16:44 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="xxx.xxx.api.beans.Notification" table="notification" catalog="xxx" optimistic-lock="version">
        <id name="idnotification" type="java.lang.Integer">
            <column name="idnotification" />
            <generator class="identity" />
        </id>
        <many-to-one name="notificationType" class="xxx.xxx.api.beans.NotificationType" fetch="select">
            <column name="notification_type_idnotification_type" not-null="true" />
        </many-to-one>
        <many-to-one name="product" class="xxx.xxx.api.beans.Product" fetch="select">
            <column name="product_idproduct" not-null="true" />
        </many-to-one>
        <many-to-one name="user" class="xxx.xxx.api.beans.User" fetch="select">
            <column name="user_iduser" not-null="true" />
        </many-to-one>
        <property name="deleteTimestamp" type="timestamp">
            <column name="delete_timestamp" length="19" />
        </property>
        <property name="dateCreated" type="timestamp">
            <column name="date_created" length="19" />
        </property>
        <property name="lastUpdated" type="timestamp">
            <column name="last_updated" length="19" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

下面是notificationSQL表

CREATE TABLE IF NOT EXISTS `notification` (
  `idnotification` INT NOT NULL AUTO_INCREMENT,
  `user_iduser` INT NOT NULL,
  `product_idproduct` INT NOT NULL,
  `notification_type_idnotification_type` INT NOT NULL,
  `delete_timestamp` TIMESTAMP NULL,
  `date_created` TIMESTAMP NULL,
  `last_updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`idnotification`),
  INDEX `fk_notification_user1_idx` (`user_iduser` ASC),
  INDEX `fk_notification_product1_idx` (`product_idproduct` ASC),
  INDEX `fk_notification_notification_type1_idx` (`notification_type_idnotification_type` ASC),
  CONSTRAINT `fk_notification_user1`
    FOREIGN KEY (`user_iduser`)
    REFERENCES `user` (`iduser`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_notification_product1`
    FOREIGN KEY (`product_idproduct`)
    REFERENCES `product` (`idproduct`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_notification_notification_type1`
    FOREIGN KEY (`notification_type_idnotification_type`)
    REFERENCES `notification_type` (`idnotification_type`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)

当我getActiveNotifications通过以下URL(http:// localhost:8080 / XXX / rest / notification / getActiveNotifications运行该方法时,出现以下错误。

org.hibernate.hql.internal.ast.QuerySyntaxException: notification is not mapped [from notification where delete_timestamp IS NULL]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
    at xxx.xxx.api.dao.notification.NotificationDAOImpl.getActiveNotifications(NotificationDAOImpl.java:101)
    at xxx.xxx.api.service.NotificationService.getActiveNotifications(NotificationService.java:299)
    at xxx.xxx.api.rest.NotificationJSONService.getActiveNotifications(NotificationJSONService.java:114)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:243)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    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:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    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:668)
    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:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: notification is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3633)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    ... 58 more

这是什么问题?另外请注意在MySQL查询中我同时使用了delete_timestamp和,deleteTimestamp但是都以相同的错误结束。

路易吉·门多萨

在Hibernate(和JPA)中,映射是到类的,即NotificationHQL使用您的实体类工作,并且区分大小写。

考虑到这一点,您的HQL查询

from notification where delete_timestamp IS NULL

应该

from Notification where deleteTimestamp IS NULL
     ^                  ^ 
     |---> class        |---> attribute in your class

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Hibernate QuerySyntaxException表未映射

Hibernate QuerySyntaxException,表未映射

org.hibernate.hql.internal.ast.QuerySyntaxException:未映射表

org.hibernate.hql.ast.QuerySyntaxException:表名未映射

Hibernate-org.hibernate.hql.internal.ast.QuerySyntaxException:客户端未映射

为什么出现“ org.hibernate.hql.internal.ast.QuerySyntaxException:未映射客户(来自客户)”错误

Spring,Spring数据JPA:org.hibernate.hql.internal.ast.QuerySyntaxException:测试未映射

我有一个org.hibernate.hql.internal.ast.QuerySyntaxException:未映射异常

休眠JPA错误:org.hibernate.hql.internal.ast.QuerySyntaxException:用户未映射

IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: ClassName 未映射

Spring Boot多模块项目org.hibernate.hql.internal.ast.QuerySyntaxException:未映射

Spring Batch-由:org.hibernate.hql.internal.ast.QuerySyntaxException引起:未映射

错误:org.hibernate.hql.internal.ast.QuerySyntaxException:网站未映射

java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:未映射用户(来自User)

Spring Boot org.hibernate.hql.internal.ast.QuerySyntaxException:未映射Utilisateur(使用MYSQL)

IllegalArgumentException-休眠错误-org.hibernate.hql.internal.ast.QuerySyntaxException:未映射Clasa [从Clasa c中选择SELECT c]

org.hibernate.hql.internal.ast.QuerySyntaxException:未映射EdbmsEmployee [来自EdbmsEmployee edbmsEmployee,其中edbmsEmployee.employeeid =?]

如果没有带有“ .setAnnotatedClasses()”的xml,则无法将类映射到Hibernate上出现“ 2QuerySyntaxException:FileEntity未映射”错误

Hibernate 5“未映射”异常

JPA映射:“ QuerySyntaxException:未映射foobar ...”

JPA 映射:“QuerySyntaxException:FooBar 未映射……”

Hibernate引发奇怪的错误:类未映射

Spring Boot + Hibernate-用户未映射

Java Hibernate错误-未映射类

JpaRepository计数查询QuerySyntaxException实体未映射

映射的Hibernate NULL值未添加(XML映射)

如何修复HIbernate 5.4.17中的实体未映射错误?

Spring Boot Hibernate-使用@OneToMany定位未映射的类

JPA Hibernate OneToMany枚举-未映射的类错误