Tam
Tam

Reputation: 12042

org.hibernate.hql.ast.QuerySyntaxException with Hibernate

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:

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

Upvotes: 12

Views: 42320

Answers (7)

Iñigo Panera
Iñigo Panera

Reputation: 49

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.

This jpql was wrong

SELECT u FROM user u WHERE userId = 1?

But this one works fine

SELECT u FROM User u WHERE userId = 1?

Upvotes: 0

Z.I.J
Z.I.J

Reputation: 1147

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

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

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

Upvotes: 0

Shanky Munjal
Shanky Munjal

Reputation: 671

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-3 to gradle-1.0-milestone-4 as 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

Upvotes: 0

Fabio
Fabio

Reputation: 11

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.

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

Upvotes: 1

chesterbr
chesterbr

Reputation: 3050

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

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. If only there was an annotation-based way to specify that...

Upvotes: 6

javashlook
javashlook

Reputation: 10461

Try adding a class element under persistence-unit, in your persistence.xml file.

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

Upvotes: 12

Mike Pone
Mike Pone

Reputation: 19320

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

Upvotes: 0

Related Questions