代码之家  ›  专栏  ›  技术社区  ›  PeakGen

Hibernate QuerySyntaxException:未映射

  •  1
  • PeakGen  · 技术社区  · 6 年前

    我正在使用 Jersey Hibernate . 数据库是 MySQL . 我的设计中有两层 dao layer , service layer REST layer

    NotificationJSONService.java (休息层)

    @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;
        }
    

    (DAO层)

    @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 . 以下是 通知 bean信息。

    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>
    

    以下是 通知 SQL表

    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 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 但两人最终都犯了同样的错误。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Luiggi Mendoza    6 年前

    在Hibernate(和JPA)中,映射到类,即, Notification . HQL使用实体类工作,并且区分大小写。

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

    from notification where delete_timestamp IS NULL
    

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