User.javapackage onlyfun.caterpillar;public class User {
    private String id;
    private String name;
    private char sex;
    private int age;    public int getAge() {
        return age;
    }    public String getId() {
        return id;
    }    public String getName() {
        return name;
    }    public char getSex() {
        return sex;
    }    public void setAge(int i) {
        age = i;
    }    public void setId(String string) {
        id = string;
    }    public void setName(String string) {
        name = string;
    }    public void setSex(char c) {
        sex = c;
    }
}hibernate.cfg.xml
<?xml version='1.0' encoding='big5'?> 
<!DOCTYPE hibernate-configuration 
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration>     <session-factory>         <!-- 顯示實際操作資料庫時的SQL --> 
        <property name="show_sql">true</property> 
        <!-- SQL方言,這邊設定的是MySQL --> 
        <!--<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>-->
        <!-- SQL方言,這邊設定的是SQLServer --> 
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>

        <!-- JDBC驅動程式 --> 
        <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property> 
        <!-- JDBC URL --> 
        <!--<property name="connection.url">jdbc:mysql://localhost/HibernateTest</property> -->
         <!-- JDBC URL --> 
        <property name="connection.url">jdbc:microsoft:sqlserver://192.168.1.51:1433;DatabaseName=audit</property> 
        <!-- 資料庫使用者 --> 
        <property name="connection.username">peizhiyou</property> 
        <!-- 資料庫密碼 --> 
        <property name="connection.password">admin123</property> 
<property name="database.schema">dbo</property>
<property name="database.catalog">Northwind</property>
<property name="c3p0.min_size">5</property> 
        <property name="c3p0.max_size">20</property> 
        <property name="c3p0.timeout">1800</property> 
        <property name="c3p0.max_statements">50</property> 
        <!-- 物件與資料庫表格映射文件 --> 
        <mapping resource="User.hbm.xml"/>     </session-factory> </hibernate-configuration>User.hbm.xml<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping 
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping>     <class name="onlyfun.caterpillar.User" table="USER">         <id name="id" type="string" unsaved-value="null"> 
            <column name="user_id" sql-type="char(32)" /> 
            <generator class="uuid.hex"/> 
        </id>         <property name="name" type="string" not-null="true"> 
            <column name="name" length="16" not-null="true"/> 
        </property>         <property name="sex" type="char"/>         <property name="age" type="int"/>     </class> </hibernate-mapping>错误信息为:
Hibernate: select user0_.user_id as user_id, user0_.name as name, user0_.sex as sex, user0_.age as age from USER user0_
Hit uncaught exception net.sf.hibernate.exception.GenericJDBCException
net.sf.hibernate.exception.GenericJDBCException: Could not execute query
at net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:80)
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4131)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1557)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1523)
at HibernateTest.query(HibernateTest.java:27)
at HibernateTest.main(HibernateTest.java:9)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 'USER' 附近有语法错误。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:89)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:880)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:273)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
... 4 more
苦等

解决方案 »

  1.   

    大哥这个帖子也顺便帮我看看:
    http://community.csdn.net/Expert/topic/4847/4847668.xml
      

  2.   

    USER 在 sqlServer中是关键字 加个单引号看看
      

  3.   

    你在SQL语句中"from onlyfun.caterpillar.User"的查询目标是一个类不是表明所以会出错
    解决办法:
        1、把查询目标换成表的名字
        Session session = sessionFactory.openSession();
        List users = session.find("from User");
        2、使用HQL
        Session session = sessionFactory.openSession();
        List users = session.createQuery("from onlyfun.caterpillar.User");
      

  4.   

    不要建表名为USER,很多数据库其都是保留的,
    改表名为USERS,其他做相应修改