这个简单的HQL语法有什么问题吗?

时间:2010-09-14 12:45:59

标签: java hibernate orm hibernate-search

您好我正在尝试使用以下方法执行此简单的HQL查询以从我的数据库中获取文件列表:

private static final String SQL_GET_FILE_LIST = "select filename, size, id, type from fileobject";

@Override
public List<FileObject> getFileList(String type) {
    // TODO Auto-generated method stub
    List<FileObject> files = hbTemplate.find(SQL_GET_FILE_LIST);
    hbTemplate.flush();
    return files;
}

但我一直得到这个例外:

  org.hibernate.hql.ast.QuerySyntaxException: fileobject is not mapped [select filename, size, id, type from fileobject]
    org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
    org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
    org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
    org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:257)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
    org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
    org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
    org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
    org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
    org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
    org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
    org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:923)
    org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
    org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
    org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)
    com.kc.models.DbFileHelper.getFileList(DbFileHelper.java:65)
    com.kc.models.FileHelper.getFileList(FileHelper.java:108)
    com.kc.Controllers.AppListController.getFileList(AppListController.java:63)
    com.kc.Controllers.AppListController.handleRequest(AppListController.java:34)
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我的hbm.xml文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.kc.models.FileObject" >

    <class name="com.kc.models.FileObject" table="fileobject">
        <id name="id" column="ID">
            <generator class="native" />
        </id>
        <property name="filename" type="string" column="FILENAME" />
        <property name="type" type="string" column="TYPE" />
        <property name="size" type="double" column="SIZE" />
        <property name="file" type="blob" length="1000000000" column="FILE" />
    </class> 

</hibernate-mapping> 

2 个答案:

答案 0 :(得分:7)

嗯,首先,正如追踪所说,fileobject is not mapped但是FileObject。因此,将您的HQL查询更改为:

select filename, size, id, type from FileObject

其次,由于您正在使用投影,因此您不会获得List<FileObject>而是List<Object[]>。所以你的代码应该是:

private static final String SQL_GET_FILE_LIST = "select filename, size, id, type from FileObject";

@Override
public List<Object[]> getFileList(String type) {
    // TODO Auto-generated method stub
    List<Object[]> files = hbTemplate.find(SQL_GET_FILE_LIST);
    hbTemplate.flush();
    return files;
}

或者,如果要获得类型安全的结果,可以使用构造函数表达式(假设您的类提供了适当的构造函数):

select new FileObject(filename, size, id, type) from FileObject

然后你可以使用你当前的代码。

private static final String SQL_GET_FILE_LIST = "select new FileObject(filename, size, id, type) from FileObject";

@Override
public List<FileObject> getFileList(String type) {
    // TODO Auto-generated method stub
    List<FileObject> files = hbTemplate.find(SQL_GET_FILE_LIST);
    hbTemplate.flush();
    return files;
}

参考

答案 1 :(得分:3)

您指定的是表名,而不是类名;他们的情况有所不同。试试这个:

private static final String SQL_GET_FILE_LIST = 
     "select filename, size, id, type from FileObject";

不可否认,自从我做了任何Hibernate以来已经有一段时间了,但我怀疑这就是问题......