标签:style blog http java color strong
报错提示为:org.hibernate.hql.ast.QuerySyntaxException: customers is not mapped [from customers as c]
服务层代码 为:
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();
}
}
hibernate配置文件为:
<?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>
而出现这个错误的根本原因是hql语法里面是POJO对象而不是table.所以改成这样就可以了:
Query query = session.createQuery("from Customers as c");
hibernate:is not mapped,布布扣,bubuko.com
标签:style blog http java color strong
原文地址:http://www.cnblogs.com/aijindi/p/3812265.html