试试
       <property name="hibernate.connection.url"><![CDATA[jdbc:mysql://localhost/hibernate?useUnicode=true&characterEncoding=GBK]]></property>

解决方案 »

  1.   

    http://www.hibernate.org/hib_docs/reference/html/session-configuration.html
    上面讲的有,
    <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
    这句话是调用那种库的dialect,你好像没有加入,
    这两个没有什么本质区别,调用的方式都差不多,知识文件格式有点变化。
      

  2.   

    The user must supply a JDBC connection
    "
    你是希望使用Jndi的数据源吧。
    用这个:
    <property name="connection.datasource">java:comp/env/jdbc/MySqlDS</property>不过,你用这种方式得到的Connection,只能打开一个Session,官方文档。
    建议使用Hibernate自己获取连接。最好问一下Totodo,他很了解Hibernate,上面都是他以前教我的。
      

  3.   

    我只是试用一下,正式项目时候肯定要用app server带的连接池
    连接池的配置我都清楚不明白的就是用hibernate.cfg.xml就是不行谢谢楼上几位的回答,我用的是2.1中lib里的jar
    Totodo,你在哪呢?:)
      

  4.   

    我记得Hibernate的官方文档说,用AppServer提供的Jndi JDBC Connection好像是
    不支持并行Session的。
    3.3. 用户自行提供JDBC连接
    SessionFactory可以使用一个用户自行提供的JDBC连接来打开一个Session。这种设计可以让应用程序来自己管理JDBC连接。应用程序必须小心,不能在同一个连接上打开多个并行的session。 java.sql.Connection conn = datasource.getConnection();
    Session sess = sessions.openSession(conn);// start a new transaction (optional)
    Transaction tx = sess.beginTransaction();
    上面的最后一行是可选的——应用程序也可能选择自行管理JTA或者JDBC事务。当然,假若你使用Hibernate Transaction,你的客户代码就可以从底层的实现中抽象出来了。(比如说,你可以将来在需要的时候切换到CORBA连接,而不需要更改程序代码。) 3.4. Hibernate提供的JDBC连接
    另一种方法就是,你可以让SessionFactory替你打开连接。SessionFactory必须事先知道连接的参数,有几种不同的方法设置参数: 传递一个java.util.Properties到Configuration.setProperties()方法。 在classpath的根目录中提供hibernate.properties文件。 通过java -Dproperty=value指定使用系统属性。 在hibernate.cfg.xml文件中包含<property>元素。详情见后。 如果你使用这种方法,打开一个Session是非常简单的: Session sess = sessions.openSession(); // obtain a JDBC connection and
                                           // instantiate a new Session
    // start a new transaction (optional)
    Transaction tx = sess.beginTransaction();
    所有的Hibernate属性名和约束都在net.sf.hibernate.cfg.Environment类中定义。我们讨论一下最重要的几项设置: 假若你设置了如下的属性,Hibernate会使用java.sql.DriverManager来得到连接,并建立连接池: Table 3.1. Hibernate JDBC属性属性名 用途 
    hibernate.connection.driver_class jdbc驱动类 
    hibernate.connection.url jdbc URL 
    hibernate.connection.username 数据库用户名 
    hibernate.connection.password 数据库用户密码 
    hibernate.connection.pool_size 连接池容量最大数 Hibernate的连接池算法是非常可配置的。它的用途是让你上手,但是并非让你在生产系统中使用的,甚至不是用来做性能测试的。 C3P0是随Hibernate发行包一起发布的一个开放源代码JDBC连接池,你可以在lib 目录中找到。假若你设置了hibernate.c3p0.* 属性,Hibernate会使用内置的C3P0ConnectionProvider作为连接池。 对Apache DBCP和Proxool的支持也是内置的。你必须设置hibernate.dbcp.*属性 (DBCP连接池属性)和hibernate.dbcp.ps.* (DBCP 语句缓存属性)才能使用DBCPConnectionProvider。要知道它们的含义,请查阅Apache commons-pool的文档。如果你想要用Proxool,你需要设置hibernate.proxool.*系列属性。 在Application Server内使用时,Hibernate可以从JNDI中注册的javax.sql.Datasource取得连接。需要设置如下属性: Table 3.2. Hibernate 数据源(Datasource)属性属性名 用途 
    hibernate.connection.datasource datasource JNDI 名字 
    hibernate.jndi.url JNDI 提供者的URL (可选)  
    hibernate.jndi.class JNDI InitialContextFactory的类名 (可选)  
    hibernate.connection.username 数据库用户名 (可选)  
    hibernate.connection.password 数据库密码 (可选)  
      

  5.   

    你可以发消息给Totodo啊。
    他是Java Java Enterprise Open Source的版主。我为有Totodo是我的同学而感到荣幸!嘿嘿。用hibernate.cfg.xml是可以的。
    你应该是我上面说的那个地方设置错了。
      

  6.   

    这么说吧,用hibernate.cfg.xml的时候系统找不到
    用hibernate.properties的时候,系统可以找到我觉得这才是问题的重点,不知道谁有这方面的经验?
    难道非要我在程序中加载?
      

  7.   

    我把详细的信息贴出来:10:22:55,702  INFO Environment:462 - Hibernate 2.1.210:22:55,713  INFO Environment:491 - hibernate.properties not found10:22:55,733  INFO Environment:519 - using CGLIB reflection optimizer10:22:55,733  INFO Environment:530 - JVM does not support Statement.getGeneratedKeys()10:22:55,733  INFO Environment:541 - JVM does not support LinkedHasMap, LinkedHashSet - ordered maps and sets disabled10:22:55,743  INFO Environment:544 - using workaround for JVM bug in java.sql.Timestamp10:22:55,813  INFO Configuration:329 - Mapping resource: bean/Person.hbm.xml10:22:57,435  INFO Binder:229 - Mapping class: bean.Person -> persons10:22:57,685  INFO Configuration:329 - Mapping resource: bean/Province.hbm.xml10:22:57,836  INFO Binder:229 - Mapping class: bean.Province -> provinces10:22:57,846  INFO Configuration:329 - Mapping resource: bean/Company.hbm.xml10:22:57,936  INFO Binder:229 - Mapping class: bean.Company -> companys10:22:58,006  INFO Configuration:595 - processing one-to-many association mappings10:22:58,006  INFO Binder:1154 - Mapping collection: bean.Company.persons -> persons10:22:58,146  INFO Configuration:604 - processing one-to-one association property references10:22:58,146  INFO Configuration:629 - processing foreign key constraints10:22:58,196  WARN SettingsFactory:50 - No dialect set - using GenericDialect: The dialect was not set. Set the property hibernate.dialect.10:22:58,196  INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.GenericDialect10:22:58,196  INFO SettingsFactory:62 - Use outer join fetching: false10:22:58,206  WARN UserSuppliedConnectionProvider:25 - No connection properties specified - the user must supply JDBC connections10:22:58,216  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)10:22:58,226  INFO SettingsFactory:102 - Use scrollable result sets: false10:22:58,226  INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false10:22:58,226  INFO SettingsFactory:108 - Optimize cache for minimal puts: false10:22:58,226  INFO SettingsFactory:117 - Query language substitutions: {}10:22:58,226  INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider10:22:58,236  INFO Configuration:1080 - instantiating and configuring caches10:22:58,657  INFO SessionFactoryImpl:119 - building session factory10:23:00,089  INFO SessionFactoryObjectFactory:82 - no JNDI name configuredok1java.lang.UnsupportedOperationException: The user must supply a JDBC connection at net.sf.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:32) at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:278) at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3264) at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3244) at net.sf.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:40) at net.sf.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:19) at net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2195) at ui.Main.testAddCompany(Main.java:53) at ui.Main.<init>(Main.java:36) at ui.Main.main(Main.java:68)Exception in thread "main"
      

  8.   

    下面是测试程序,bean类没问题/**
     * Hibernate Demo
     * Copyright by cinc
     */
    package ui;import java.sql.SQLException;
    import net.sf.hibernate.Hibernate;
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.MappingException;
    import net.sf.hibernate.SessionFactory;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.Transaction;
    import net.sf.hibernate.cfg.Configuration;import bean.Person;
    import bean.Province;
    import bean.Company;/**
     * This is just a simple demo for accessing hibernate
     * Detailed demo please see tests : \src\test\dao\hibernate\*Test.java
     */
    public class Main {
        private SessionFactory sessionFactory;    public Main(){
            try {
        System.out.println("ok");
                Configuration conf = new Configuration()
                         .addClass( Person.class )
                         .addClass( Province.class )
                         .addClass( Company.class );
                sessionFactory = conf.buildSessionFactory();
                System.out.println("ok1");
                testAddCompany();        } catch (MappingException e) {
                e.printStackTrace();
            } catch (HibernateException e) {
                e.printStackTrace();
            }
        }    public void testAddCompany() throws HibernateException{
            Company company = new Company();
            company.setName( "company name" );
    company.setId( "TEST1" );        Session s = sessionFactory.openSession();
            Transaction tx = null;
            try{
                tx = s.beginTransaction();
                s.save( company );
                tx.commit();
                System.out.println( "Save company to db ok." );
            }catch(HibernateException he){
                if ( tx!=null ){
                    tx.rollback();
                }
                throw he;
            }finally{
                s.close();
            }
        }    public static void main(String args[]){
            new Main();
        }
    } //EOC
      

  9.   

    我觉得出现
    "java.lang.UnsupportedOperationException: The user must supply a JDBC connection
    不是你的程序的问题,而是你的配置出了问题,
    我前一阵子也是这个问题。后来通过修改我上面说的,就可以了。
    不过因为不能支持并行Session,所以我又后悔了嘿。你还是仔细找找你的配置吧?我都没看你代码,就猜测了一下。
    错了不要怪我。还是找Totodo好。
      

  10.   

    <?xml  version='1.0'  encoding='utf-8'?>  
    <!DOCTYPE  hibernate-configuration  
           PUBLIC  "-//Hibernate/Hibernate  Configuration  DTD//EN"  
           "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">  
    <hibernate-configuration>  
           <session-factory>  
                   <property  name="connection.driver_class">org.gjt.mm.mysql.Driver</property>  
                   <property  name="connection.url">jdbc:mysql://localhost/hibernate?useUnicode=true&characterEncoding=GBK</property>  
                   <property  name="connection.usename">hibernate</property>  
               <property  name="connection.password">hibernate</property>  
                   <property  name="connection.pool_size">1</property>  
                   <property  name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>   
           </session-factory>  
    </hibernate-configuration> 和我的hibernate.properties中表达的意思一致,就是连一个jdbc驱动,实在看不出错在哪
    hibernate.properties非常流畅
    而hibernate.cfg.xml却视而不见难道对hibernate.cfg.xml并不是默认支持,必须要在代码中加载?
      

  11.   

    不用。每个刚接触Hibernate的人,
    都是会碰到这些配置问题的。
    都配置几次就通了。哈。
      

  12.   

    对了。如果你使用了hibernate.cfg.xml,你要把
    hibernate.properties
    里的相关信息注释掉。
      

  13.   

    哈哈 问题出在哪?不在配置 出在你的代码中!
    new Configuration()
    默认使用的是hibernate.properties
    因为你没有 所以这时log会打出10:22:55,713  INFO Environment:491 - hibernate.properties not found这时你要使用了hibernate.cfg.xml只要这样些就可以了
    new Configuration().configure()就可以加载进来了!试试看:)
      

  14.   

    Sorry,今天有事出去了。 
     public Main(){
            try {
        System.out.println("ok");
           /**
                Configuration conf = new Configuration()
                         .addClass( Person.class )
                         .addClass( Province.class )
                         .addClass( Company.class );
           **/
           //注意查看那 Configuration的构造方式。。
           //当然你也可以。。File file = new File("c:/hibernate.cfg.xml");
            //             session=conf.configure(file).buildSessionFactory().通过指定xml文件加载。 
            Configuration conf = new Configuration();
            sessions = conf.configure().buildSessionFactory(); 
                sessionFactory = conf.buildSessionFactory();
                System.out.println("ok1");
                testAddCompany();        } catch (MappingException e) {
                e.printStackTrace();
            } catch (HibernateException e) {
                e.printStackTrace();
            }
        }
    照你的代码看,你需要把hibernate.hbm.xml 改成以下方式。。<?xml  version='1.0'  encoding='utf-8'?>  
    <!DOCTYPE  hibernate-configuration  
           PUBLIC  "-//Hibernate/Hibernate  Configuration  DTD//EN"  
           "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">  
    <hibernate-configuration>  
           <session-factory>  
                   <property  name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>  
                   <property  name="hibernate.connection.url">jdbc:mysql://localhost/hibernate?useUnicode=true&characterEncoding=GBK</property>  
                   <property  name="hibernate.connection.usename">hibernate</property>  
               <property  name="hibernate.connection.password">hibernate</property>  
                   <property  name="hibernate.connection.pool_size">1</property>  
                   <property  name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>  
                   <!--  Mapping  files  -->  
          <mapping resource="Company.hbm.xml"/> 
              <mapping resource="Person.hbm.xml"/> 
               <mapping resource="Province.hbm.xml"/>  
           </session-factory>  
    </hibernate-configuration>
      

  15.   

    xmvigour(微电-THINKING IN APP)  讲的是对的:)。
      

  16.   

    谢谢,xml文件是找到了,但又有以下错误:net.sf.hibernate.MappingException: duplicate import: Company at net.sf.hibernate.cfg.Mappings.addImport(Mappings.java:85) at net.sf.hibernate.cfg.Binder.bindClass(Binder.java:126) at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:221) at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1229) at net.sf.hibernate.cfg.Configuration.add(Configuration.java:249) at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:285) at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:315) at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:976) at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:928) at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:856) at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:842) at ui.Main.<init>(Main.java:34) at ui.Main.main(Main.java:69)希望能够继续回答,谢谢了
      

  17.   


    ok,搞定,谢谢楼上的兄弟们
    刚接触hibernate,以后还要请大家多多帮忙了
    :)
      

  18.   

    zxhong(红透半边天) ,
    哈,你过谦了啊。向你学习:)
      

  19.   

    本来结贴的,结果说什么给分数大于回复数
    bug,呵呵,晚上再试试
    :)
      

  20.   

    Message.hbm.xml
    是啥??贴出来看看