简体   繁体   English

带有Hibernate的org.hibernate.hql.ast.QuerySyntaxException

[英]org.hibernate.hql.ast.QuerySyntaxException with Hibernate

I'm new to using Hibernate with Java. 我是使用Hibernate和Java的新手。 I'm getting the following exception. 我收到以下异常。 The stuff that I found online regarding this error didn't seem to help. 我在网上找到关于这个错误的东西似乎没有帮助。 Any ideas? 有任何想法吗? The Exception: 例外情况:

java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: 

ApplPerfStats is not mapped [select count(c) from ApplPerfStats c]
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:601)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:96)
    at com.icesoft.icefaces.samples.datatable.jpa.CustomerDAO.findTotalNumberCustomers(CustomerDAO.java:89)
    at com.icesoft.icefaces.samples.datatable.ui.SessionBean.getDataPage(SessionBean.java:189)
    at com.icesoft.icefaces.samples.datatable.ui.SessionBean.access$0(SessionBean.java:185)
    at com.icesoft.icefaces.samples.datatable.ui.SessionBean$LocalDataModel.fetchPage(SessionBean.java:245)
    at com.icesoft.icefaces.samples.datatable.ui.PagedListDataModel.getPage(PagedListDataModel.java:121)
    at com.icesoft.icefaces.samples.datatable.ui.PagedListDataModel.getRowCount(PagedListDataModel.java:100)
    at com.icesoft.faces.component.datapaginator.DataPaginator.isModelResultSet(DataPaginator.java:1091)
    at com.icesoft.faces.component.datapaginator.DataPaginatorRenderer.encodeBegin(DataPaginatorRenderer.java:201)

The place where this is called: 这叫做的地方:

@SuppressWarnings("unchecked")
public Long findTotalNumberCustomers() {
    EntityManagerHelper.log("finding number of Customer instances", Level.INFO, null);
    try {
        String queryString = "select count(c) from ApplPerfStats c";
        return (Long) getEntityManager().createQuery(queryString).getSingleResult();
    } catch (RuntimeException re) {
        EntityManagerHelper.log("find number of Appl_perf_stats failed",
                Level.SEVERE, re);
        throw re;
    }
}

The class that maps to the database table: 映射到数据库表的类:

package com.icesoft.icefaces.samples.datatable.jpa;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Appl_perf_stats", uniqueConstraints = {})
public class ApplPerfStats implements java.io.Serializable {
.....

Thanks, 谢谢,

Tam

Try adding a class element under persistence-unit , in your persistence.xml file. 尝试在persistence.xml文件中的persistence-unit下添加一个class元素。

<?xml version="1.0" encoding="UTF-8"?>
<persistence ...>

    <persistence-unit name="unit">
        <class>com.icesoft.icefaces.samples.datatable.jpa.ApplPerfStats</class>
        ...
     </persistence-unit>
<persistence>

I haven't done much more than that with JPA/EntityManager, so I don't know if there's a way to add an entire package. 我没有比JPA / EntityManager做得更多,所以我不知道是否有办法添加整个包。 AFAIK, when using hibernate.cfg.xml , each persistent class has to be specified directly. AFAIK,当使用hibernate.cfg.xml ,必须直接指定每个持久化类。

It happened to me until I started to use the full class name, eg: 在我开始使用完整的类名之前,它发生在我身上,例如:

String queryString = "select count(c) from com.my.classes.package.ApplPerfStats c";

But I don't like this approach, because it's refactoring-unfirendly. 但我不喜欢这种方法,因为它的重构 - 不友好。 A more tractable one would be: 更易处理的是:

String queryString = "select count(c) from " + ApplPerfStats.class.getName() +  c";

javashlook's solution seems to be a shortcut for that - but it adds more XML configuration, which I try to avoid. javashlook的解决方案似乎是一个捷径 - 但它增加了更多的XML配置,我试图避免。 If only there was an annotation-based way to specify that... 如果只有基于注释的方式来指定...

I was having the same problem and I solved by adding aspectj entries to my pom.xml see below. 我遇到了同样的问题,我通过在我的pom.xml中添加aspectj条目来解决,见下文。 I guess it makes sense if you are using annotations. 我想如果你使用注释是有道理的。 Otherwise you need to specify the mappings in the XML file. 否则,您需要在XML文件中指定映射。 I had this problem from a project that was using a jar with JPA annotations. 我从一个使用带有JPA注释的jar的项目中遇到了这个问题。

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.2</version> <!-- NB: do use 1.3 or 1.3.x due to MASPECTJ-90 - wait for 1.4 -->
            <dependencies>
                <!-- NB: You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
    <configuration>
        <weaveDependencies>
        <weaveDependency>                                     <groupId>your.project</groupId>                        <artifactId>your.artifact</artifactId>                     </weaveDependency>                    
        </weaveDependencies>
        </configuration>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <outxml>true</outxml>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>

I faced this issue but in my case the problem was due to gradle version. 我遇到了这个问题,但在我的情况下问题是由于gradle版本。
When I changed my system from linux to mac then I had to switch from gradle-1.0-milestone-3 to gradle-1.0-milestone-4 as milestone-3 does not work in OSX. 当我将系统从linux更改为mac时,我不得不从gradle-1.0-milestone-3切换到gradle-1.0-milestone-4,因为里程碑-3在OSX中不起作用。 And in gradle-1.0-milestone-4 I faced the same issue then I had to degrade my gradle version to gradle-1.0-milestone-1. 在gradle-1.0-milestone-4中我遇到了同样的问题,然后我不得不将我的gradle版本降级为gradle-1.0-milestone-1。 Now it is working fine 现在工作正常

I was also face this problem fixed by this ... 我也面临着这个被这个问题解决的问题......

You haven't declared your entity classes in persistence.xml config file: 您尚未在persistence.xml配置文件中声明您的实体类:

<property name="hibernate.archive.autodetection" value="class, hbm"/>

I know it has been a long time on this matter, but none of the above answers solved my problem. 我知道这件事已经很长时间了,但上述答案都没有解决我的问题。

I had already declared the entity in the persistence.xml file. 我已经在persistence.xml文件中声明了该实体。 At the end the whole problem was simply that the name of the entity is case sensitive on my system . 最后,整个问题只是实体的名称在我的系统上区分大小写

This jpql was wrong 这个jpql错了

SELECT u FROM user u WHERE userId = 1? SELECT u FROM user u WHERE userId = 1?

But this one works fine 但是这个工作正常

SELECT u FROM User u WHERE userId = 1? SELECT u FROM User u WHERE userId = 1?

You should specify a column to do the count on 您应该指定一个列来进行计数

select count(c.someColumn) from ApplPerfStats c

Or try a count(*) 或尝试计数(*)

select count(*) from ApplPerfStats c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM