Java org.hibernate.hql.ast.QuerySyntaxException 与 Hibernate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/854978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:14:12  来源:igfitidea点击:

org.hibernate.hql.ast.QuerySyntaxException with Hibernate

javahibernate

提问by Tam

I'm new to using Hibernate with 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:

我是将 Hibernate 与 Java 结合使用的新手。我收到以下异常。我在网上找到的有关此错误的内容似乎没有帮助。有任何想法吗?例外:

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

叫这个的地方:

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 {
.....

The class that maps to the database table:

映射到数据库表的类:

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

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

Thanks,

谢谢,

Tam

采纳答案by javashlook

Try adding a classelement under persistence-unit, in your persistence.xmlfile.

尝试在文件中的,class下添加一个元素。persistence-unitpersistence.xml

select count(c.someColumn) from ApplPerfStats c

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. AFAIK, when using hibernate.cfg.xml, each persistent class has to be specified directly.

我对 JPA/EntityManager 没有做更多的事情,所以我不知道是否有办法添加整个包。AFAIK,在使用时hibernate.cfg.xml,必须直接指定每个持久类。

回答by Mike Pone

You should specify a column to do the count on

您应该指定一列来进行计数

select count(*) from ApplPerfStats c

Or try a count(*)

或尝试计数(*)

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

回答by chesterbr

It happened to me until I started to use the full class name, e.g.:

它发生在我身上,直到我开始使用完整的类名,例如:

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

But I don't like this approach, because it's refactoring-unfirendly. A more tractable one would be:

但我不喜欢这种方法,因为它重构不友好。一个更容易处理的将是:

        <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>

javashlook's solution seems to be a shortcut for that - but it adds more XML configuration, which I try to avoid. If only there was an annotation-based way to specify that...

javashlook 的解决方案似乎是一个捷径 - 但它添加了更多的 XML 配置,我尽量避免。如果只有一种基于注释的方式来指定...

回答by Fabio

I was having the same problem and I solved by adding aspectj entries to my pom.xml see below. I guess it makes sense if you are using annotations. Otherwise you need to specify the mappings in the XML file. I had this problem from a project that was using a jar with JPA annotations.

我遇到了同样的问题,我通过在 pom.xml 中添加 aspectj 条目来解决,见下文。如果您使用注释,我想这是有道理的。否则,您需要在 XML 文件中指定映射。我在一个使用带有 JPA 注释的 jar 的项目中遇到了这个问题。

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

回答by Shanky Munjal

I faced this issue but in my case the problem was due to gradle version.
When I changed my system from linux to mac then I had to switch from gradle-1.0-milestone-3to gradle-1.0-milestone-4as milestone-3 does not work in 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. Now it is working fine

我遇到了这个问题,但就我而言,问题是由于 gradle 版本。
当我将系统从 linux 更改为 mac 时,我不得不从gradle-1.0-milestone-3切换到gradle-1.0-milestone-4,因为里程碑 3 在 OSX 中不起作用。在 gradle-1.0-milestone-4 中,我遇到了同样的问题,然后我不得不将我的 gradle 版本降级为 gradle-1.0-milestone-1。现在它工作正常

回答by Z.I.J

I was also face this problem fixed by this ...

我也面临这个问题解决了这个......

You haven't declared your entity classes in persistence.xml config file:

您尚未在 persistence.xml 配置文件中声明您的实体类:

##代码##

回答by I?igo Panera

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. At the end the whole problem was simply that the name of the entity is case sensitive on my system.

我已经在persistence.xml 文件中声明了实体。最后,整个问题只是实体的名称在我的系统上区分大小写

This jpql was wrong

这个jpql错了

SELECT u FROM useru WHERE userId = 1?

SELECT u FROM useru WHERE userId = 1?

But this one works fine

但是这个很好用

SELECT u FROM Useru WHERE userId = 1?

SELECT u FROM Useru WHERE userId = 1?