tags:

views:

3305

answers:

3

Hello,

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

A: 

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
Mike Pone
Thanks Mike. I tried both of them but still got the same exception :(
Tam
make sure you're not missing anything in your annotations. After looking closer it looks like Hibernate doesn't think that class is mapped to a table.
Mike Pone
+2  A: 

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.

javashlook
A: 

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

chester