我的工具 myeclipse 8.5 +oracle 10g
1.实体类映射文件Student.hbm.xml
[code="java"]<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
[/code]
2.hibernate配置文件hibernate.cfg.xml
[code="java"]<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
true
org.hibernate.dialect.Oracle9Dialect
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:softeem
java1103
java1103
<mapping resource="com/softeem/pojo/Student.hbm.xml" />
[/code]
3.test测试类CURDTest.java
[code="java"]package com.softeem.test;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.softeem.pojo.Student;
import junit.framework.TestCase;
public class CURDTest extends TestCase{
//增
public void insertTest(){
Configuration config=new Configuration().configure();
SessionFactory sf=config.buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
//增加十条记录
for(int i=0;i<10;i++){
Student stu=new Student();
stu.setSname("顾超");
stu.setSsex("男");
stu.setSage(21);
stu.setStime(new Date());
session.save(stu);
}
session.getTransaction().commit();
session.close();
}
//删
public void deleteTest(){
Configuration config=new Configuration().configure();
SessionFactory sf=config.buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
Student stu=(Student) session.load(Student.class, 1);
session.delete(stu);
session.getTransaction().commit();
session.close();
}
//改
public void updateTest(){
Configuration config=new Configuration().configure();
SessionFactory sf=config.buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
Student stu=(Student) session.load(Student.class, 2);
stu.setSname("小强");
stu.setSage(1);
session.update(stu);
session.getTransaction().commit();
session.close();
}
//查
public void selectTest(){
Configuration config=new Configuration().configure();
SessionFactory sf=config.buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
Student stu=(Student) session.load(Student.class,2);
System.out.println(stu.getSno()+" "+stu.getSname()+" "+stu.getSsex()+" "+stu.getSage()+" "+stu.getStime());
//Query query=new Query("from tb_student");
session.getTransaction().commit();
session.close();
}
// //查找全部
// public void selectAllTest(){
// Configuration config=new Configuration().configure();
// SessionFactory sf=config.buildSessionFactory();
// Session session=sf.openSession();
// session.beginTransaction();
// Query query=session.createQuery("from tb_student");
// List<?> list=query.list();
// Iterator<?> it=list.iterator();
// while(it.hasNext()){
// Student stu=(Student) it.next();
// System.out.println(stu.getSno()+" "+stu.getSname()+" "+stu.getSsex()+" "+stu.getSage()+" "+stu.getStime());
// }
// session.getTransaction().commit();
// session.close();
// }
//
// //分页
// public void pagingTest(){
// Configuration config=new Configuration().configure();
// SessionFactory sf=config.buildSessionFactory();
// Session session=sf.openSession();
// session.beginTransaction();
// Query query=session.createQuery("form tb_student");
// query.setFirstResult(3);
// query.setMaxResults(3);
// List<?> list=query.list();
// for(int i=0;i
// System.out.println(list.get(i));
// }
// session.getTransaction().commit();
// session.close();
// }
//
// //模糊查询
// public void blurTest(){
// Configuration config=new Configuration().configure();
// SessionFactory sf=config.buildSessionFactory();
// Session session=sf.openSession();
// session.beginTransaction();
// Query query=session.createQuery("form tb_student where sname like ?");
// query.setString(0, "%小%");
// List> list=query.list();
// for(int i=0;i<list.size();i++){
// System.out.println(list.get(i));
// }
// session.getTransaction().commit();
// session.close();
// }
}
[/code]
问题是:
[code="java"]Query query=session.createQuery("from tb_student");[/code]这条语句会报错
当我改成:
[code="java"]Query query=session.createSQLQuery("select * from tb_student");[/code]这条语句不会报错,但是
[code="java"]Student stu=(Student) it.next();
[/code]这条语句就会报错.
不知道什么原因,纠结了很久。请大家指点。