使用Hibernate为Primefaces数据表延迟加载rowcount错误

时间:2012-12-19 10:10:11

标签: java spring hibernate jpa

我正在尝试为我的JSF数据表实现延迟加载,我的应用程序正在使用JSF2.0。 Spring 3和Hibernate 4。

我在DAO中有以下内容

@Override
public int getRequestCount() {          
    Query query = entityManager.createNamedQuery("Request.count");
    return ((Long) query.getSingleResult()).intValue();
}

在ManagedBean中我有

@Named("reqMB")
@Scope("request")
public class RequestManagedBean implements Serializable {

// other code .....    
lazyModel.setRowCount(getRequestService().getRequestCount());
....
return lazyModel;

在实体类

@Entity
@Table(name = "V_REQUESTS")
@NamedQueries({
    @NamedQuery(name = "Request.count", query = "SELECT COUNT(r) FROM <viewname> r")
})
public class Request {

我遇到的问题是当我尝试将我的应用程序部署到weblogic 10.3.6时,我收到以下异常。

Error creating bean with name 'requestDAOImpl': Injection of 
autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not 
autowire field: private org.hibernate.SessionFactory 
net.test.request.dao.RequestDAOImpl.sessionFactory; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'SessionFactory' defined in ServletContext resource 
[/WEB-INF/applicationContext.xml]: Invocation of init method failed; 
nested exception is org.hibernate.HibernateException: 
Errors in named queries: Request.count

如何解决此问题?

另一点不是使用以下内容,是否还有其他方法可以获取用于延迟加载的rowcount?

@NamedQueries({
    @NamedQuery(name = "Request.count", query = "SELECT COUNT(r) 
    FROM vw_request r")})

由于

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题,我犯了错误 而不是类名,我使用了实际的视图名称,所以我改为

@NamedQuery(name = "Request.count", query = "SELECT COUNT(r) FROM Request r").

我的另一个疑问是没有使用上面的方法来获取rowcount,我已经修改了我的DAO,如下所示,我不需要在Entity类中使用@NamedQuery。

int count = ((Long)sessionFactory.getCurrentSession().createQuery("select count(*) 
from Request").uniqueResult()).intValue();