• is not mapped 错误改正


    我出现的错误是oorg.hibernate.hql.ast.QuerySyntaxException: DEPT is not mapped [from DEPT]

    配置文件如下:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="cn.lex.entity">
    
        <class name="Dept" table="DEPT">
            <id name="deptno" column="deptno">
                <generator class="native"/>
            </id>
            <property name="dname"></property>
            <property name="loc"></property>
        </class>
    
    </hibernate-mapping
    >

    代码如下:

    package cn.lex.test;
    
    import cn.lex.entity.Dept;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import java.util.List;
    
    /**
     * Created by accp on 2017/1/9.
     */
    public class SecondTest {
        Session session;
        Transaction tx;
        @Before
        public void before(){
            Configuration cfg=new Configuration().configure();
            SessionFactory factory=cfg.buildSessionFactory();
            session=factory.openSession();
            tx=session.beginTransaction();
        }
    
        @Test
        public void page(){
            String hql="from DEPT order by DEPTNO";
            //创建query对象
           Query query = session.createQuery(hql);
           //每页显示几条数据
           int pageSize=2;
           //设置第一页
            int pageIndex=1;
           //设置每页显示的最大记录数
           query.setMaxResults(pageSize);
           //设置从第几条开始输出
           query.setFetchSize((pageIndex-1)*pageSize);
            List<Dept> list = query.list();
            for (Dept dept:list) {
                System.out.println("部门编号:"+dept.getDeptno());
            }
    
        }
    
        @After
        public void after(){
            tx.commit();
            session.close();
        }
    
    }

    而出现这个错误的根本原因是hql语法里面是POJO(Plain Ordinary Java Object)对象而不是table.所以改成这样就可以了:

    List<Dept> list=session.createQuery("from Dept order by DEPTNO”).list();

    特记于此!以备勿忘!

  • 相关阅读:
    南大算法设计与分析课程课后习题(1)
    南大算法设计与分析课程复习笔记(1) L1
    LeetCode-59. Spiral Matrix II
    LeetCode-58. Length of Last Word
    LeetCode-56. Merge Intervals
    LeetCode-55. Jump Game
    linux内核源码目录结构分析
    LeetCode-54. Spiral Matrix
    如何保证数据一致性
    CountDownLatch的简单使用
  • 原文地址:https://www.cnblogs.com/wl0000-03/p/6266322.html
Copyright © 2020-2023  润新知