iteye_9617
iteye_9617
2011-07-04 18:59
浏览 316
已采纳

hibernate中qurey查询 用Junit测试报错

我的工具 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]这条语句就会报错.

不知道什么原因,纠结了很久。请大家指点。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • iteye_4734
    iteye_4734 2011-07-05 05:15
    已采纳

    1.Query query=session.createQuery("from tb_student"); 错误的原因是由于你使用的是HQL语句,你这样写肯定错误,应该写成:Query query=session.createQuery("from Student");
    2.我感觉你的Student.hbm.xml 有问题,为什么所有的属性你都没有为他们指定类型啊。
    :D

    点赞 评论
  • iteye_8658
    iteye_8658 2011-07-04 19:04

    找本hibernate的书好好学习一下吧,这是最基本的用法,你都没有用对。

    点赞 评论
  • iteye_8658
    iteye_8658 2011-07-05 09:45

    楼上的正确了

    点赞 评论

相关推荐