码迷,mamicode.com
首页 > 移动开发 > 详细

hibernate:is not mapped

时间:2014-07-10 10:29:13      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   strong   

报错提示为:org.hibernate.hql.ast.QuerySyntaxException: customers is not mapped [from customers as c]

服务层代码 为:

bubuko.com,布布扣
public void findCustomerAndOrders(){
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery("from customers as c");
            List list = query.list();
            for(int i = 0 ;i<list.size();i++){
                Customers c = (Customers) list.get(i);
                System.out.println(":"+c.getId());
                System.out.println(":"+c.getName());
                System.out.println(":"+c.getAddress());
                Iterator it = c.getOrderses().iterator();
                while(it.hasNext()){
                    Orders order = (Orders)it.next();
                    System.out.println(order.getOrderNumber()+"  ");
                }
                System.out.println("  ");
            }
            tx.commit();
        } catch(Exception e ){
            if(tx!=null){
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
bubuko.com,布布扣

hibernate配置文件为:

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.yehancheng.bean.Customers" table="customers">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" not-null="true" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="address" not-null="true" />
        </property>
        <set name="orderses" inverse="true" cascade="all">
            <key>
                <column name="customerId" not-null="true" />
            </key>
            <one-to-many class="com.yehancheng.bean.Orders" ></one-to-many>
        </set>
    </class>
</hibernate-mapping>
bubuko.com,布布扣

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

 Query query = session.createQuery("from Customers as c");

hibernate:is not mapped,布布扣,bubuko.com

hibernate:is not mapped

标签:style   blog   http   java   color   strong   

原文地址:http://www.cnblogs.com/aijindi/p/3812265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!