Error message: Org.hibernate.hql.ast.QuerySyntaxException:customers is notmapped [from customers as C]
The service layer code is:
PublicvoidFindcustomerandorders () {Session session =Sessionfactory.opensession (); Transaction tx =Null;Try{tx =Session.begintransaction (); Query query = Session.createquery ("FromCustomers as C "); List List =Query.list ();Forint 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 ("catch (Exception e) {nullfinally {session.close ();}
Hibernate configuration files are:
<?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><ClassName= "Com.yehancheng.bean.Customers"Table= "Customers"><IdName= "id"Type= "Java.lang.Integer"><ColumnName= "id"/><GeneratorClass= "Native"/></Id><PropertyName= "Name"Type= "Java.lang.String"><ColumnName= "Name"Not-null= "true"/></Property><PropertyName= "Address"Type= "Java.lang.String"><ColumnName= "Address"Not-null= "true"/></Property><SetName= "Orderses"Inverse= "true"Cascade= "All"><Key><ColumnName= "CustomerId" Not-null= "true" /> </key> <one-to-many class = "Com.yehancheng.bean.Orders" ></one-to-many> </ set> </class></ Hibernate-mapping>
The root cause of this error is that the HQL syntax is a Pojo object instead of a table. So change it to this:
Query query = Session.createquery ("From Customers as C");