我的java代码如下,数据库是oracle
package com.frame.db.user;import javax.servlet.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import java.io.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;public class BusinessService{
  public static SessionFactory sessionFactory;
  static{
    try{
      // Create a configuration based on the properties file we've put
      // in the standard place.
      Configuration config = new Configuration();
      config.addClass(UserInfo.class);
      // Get the session factory we can use for persistence
      sessionFactory = config.buildSessionFactory();
    }catch(Exception e){e.printStackTrace();}
  }   
   public void test(ServletContext context,OutputStream out) throws Exception{    UserInfo userInfo=new UserInfo();    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      //UserInfo user=(UserInfo)session.load(UserInfo.class,new Long(1));
      Query query = session.createQuery("from user_info where user_id=1");//(UserInfo.class,new Long(1));
      //query.setLong(0, id);
      
      //List list = session.find("from user_info");
      
      List list = query.list();
      if(list.size() > 0)
      {
      UserInfo user = (UserInfo)list.get(0);
      System.out.println(user.getLogin_name());
      System.out.println(user.getUser_name());
      }
      else
      {
       System.out.println("没有找到对应的数据");  
      }
      //tx.commit();
    }catch (Exception e) {
      if (tx != null) {
        // Something went wrong; discard all partial changes
        tx.rollback();
      }
      throw e;
    } finally {
      // No matter what, close the session
      session.close();
    }  }  public static void main(String args[]) throws Exception {
    new BusinessService().test(null,System.out);
    sessionFactory.close();
  }}运行之后,出现如下错误:
log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select  from where (user_id=1 )
Exception in thread "main" net.sf.hibernate.exception.SQLGrammarException: Could not execute query
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4110)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1556)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at com.frame.db.user.BusinessService.test(BusinessService.java:41)
at com.frame.db.user.BusinessService.main(BusinessService.java:67)
Caused by: java.sql.SQLException: ORA-00936: 缺少表达式 at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:573)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1891)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:830)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2391)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2672)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:589)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:527)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:88)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1553)
... 3 more
高手们帮忙看看,我写得不对吗?

解决方案 »

  1.   

    有两个问题:1. 建议在获取Session的时候使用sessionFactory.getCurrentSession();
    2. Query query = session.createQuery("from user_info where user_id=1");这个hql语句有问题,from user_info(应该是对象名而不是表名) where user_id=1
      

  2.   

    楼上基本都说了
    我说说hql把,假设你的user_info表对应的类是UserInfo,user_id对应的是类UserInfo的主键,那么应该这样写
    select ui from UserInfo ui where ui.id = 1;
      

  3.   

    谢谢二位,hibernate你们在实际项目中,怎么封装呢?能不能给点例子?
      

  4.   

    hql语句中user_info 应该是对象名 而不是表名 
      

  5.   

    Hibernate: select from where (user_id=1 )看这句
    转换成sql语句时表名都没有嘛
    说明你java里面hql语句写错了user_info应该改成对象名吧
      

  6.   

    其实建议不要用getSession,甚至不要用session 会出很多问题,看hibernate源码就知道,用getHibernateTemplate()来做...
      

  7.   

    sessionFactory = config.buildSessionFactory();你这里好像也有问题应该是
    sessionFactory = config.configure().buildSessionFactory();
      

  8.   


    这位兄弟!你指的是spring+hibernate,那不用spring呢!getHibernateTemplate()其实也是封装对session的操作,你不需要手动关闭,你指的getSession()应该是spring自带的一个方法吧,这个方法要手动关闭session的,但楼主根本没有使用spring,所以不存在你说的这个问题,你说的是委托给spring管理session的情况下
      

  9.   

    from user_info where user_id=1改为from UserInfo u where u.userId=1 
      

  10.   

    浪曦免费大型企业门户综合项目设计和管理技术讲座讲座议题:
    一、大型企业门户综合项目设计与管理
    内容概要:软件项目需求分析
              案例:某企业门户解决方案介绍——需求调研与分析
              讲解大型企业门户系统架构设计和系统设计
              学员现场答疑二、企业门户综合集成平台
        以一个真实的门户案例为基调,讲解从接手一个门户项目,执行项目定义开始,到项目的需求分析,架构设计,系统设计,详细设计,开发实现,测试调试,直到最后的系统上线,上线监控过程中一个项目组或项目组的项目经理、工程师所做的工作。面向参会人员:
          讲座主要面向IT专业人士,大学生,以及IT技术爱好者,侧重致力于项目管理工作人员。讲座时间:2010年3月28日 上午9:30讲座地点:北京海淀科技大厦五层学术报告厅
        此次讲座由浪曦科技与鼎亚科技联合主办,是国内高水准的IT管理技术交流平台。您将有机会了解全球最新动态、把握创新机遇,分享应用经验。敬请拨冗出席! 
       (名额有限,欲参加者请于3月26日晚之前咨询QQ:86171568)