>>>>>>>>>>很简单的,大家看看,虽然长了点,但都是能一目了然的东西,请多多指教 < < < < < < < < < < < < < < < 
我用IDEA做一个hibernate的练习. 
存一条记录到数据库中 
主函数如下: 
    Configuration con = new Configuration().configure(); 
    SessionFactory sessionFactory = con.buildSessionFactory(); 
    User user = new User(); 
    Session session = sessionFactory.openSession(); 
    user.setId(100); 
    user.setName("chenaaaaaaaa"); 
    user.setPassword("123456"); 
    session.save(user); 
    session.close(); 
hibernate.cfg.xml文件如下: 
<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
  "-//Hibernate/Hibernate Configuration DTD//EN" 
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory > 
      <property name="connection.driver_class">com.sybase.jdbc3.jdbc.SybDriver </property> 
          <property name="connection.url"> 
          jdbc:sybase:Tds:172.22.80.78:5000/caiwu 
        </property> 
          <property name="connection.username">sa </property> 
          <property name="connection.password"> </property> 
          <property name="connection.pool_size">10 </property> 
          <property name = "dialect">org.hibernate.dialect.SybaseDialect </property> 
          <property name="show_sql">true </property> 
          <property name="hbm2ddl.auto">create </property> 
          <mapping resource="model/User.hbm.xml" /> </session-factory> 
</hibernate-configuration> User.hbm.xml文件如下: 
<hibernate-mapping> 
    <class name="model.User" table="users" catalog="caiwu"> 
        <id name="id" type="java.lang.Integer"> 
            <column name="id" /> 
            <generator class="assigned" /> 
        </id>         <property name="name" type="java.lang.String"> 
            <column name="name" length="45" not-null="true" /> 
        </property> 
        <property name="password" type="java.lang.String"> 
            <column name="password" length="45" not-null="true" /> 
        </property> 
        
    </class> 
</hibernate-mapping> 
控制台信息如下: 
- Hibernate 3.0 alpha 
- hibernate.properties not found 
- using CGLIB reflection optimizer 
- using JDK 1.4 java.sql.Timestamp handling 
- configuring from resource: /hibernate.cfg.xml 
- Configuration resource: /hibernate.cfg.xml 
- Mapping resource: model/User.hbm.xml 
- Mapping class: model.User -> users 
- Configured SessionFactory: null 
- processing one-to-many association mappings 
- processing one-to-one association property references 
- processing foreign key constraints 
- Using dialect: org.hibernate.dialect.SybaseDialect 
- Generate SQL with comments: disabled 
- Query language substitutions: {} 
- Using Hibernate built-in connection pool (not for production use!) 
- Hibernate connection pool size: 10 
- autocommit mode: false 
- using driver: com.sybase.jdbc3.jdbc.SybDriver at URL: jdbc:sybase:Tds:172.22.80.78:5000/caiwu 
- connection properties: {user=sa, password=****} 
- Scrollable result sets: enabled 
- JDBC3 getGeneratedKeys(): disabled 
- Using default transaction strategy (direct JDBC transactions) 
- No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended) 
- Cache provider: org.hibernate.cache.EhCacheProvider 
- Second-level cache: enabled 
- Optimize cache for minimal puts: disabled 
- Query cache: disabled 
- Echoing all SQL to stdout 
- Statistics: disabled 
- Deleted entity synthetic identifier rollback: disabled 
- building session factory 
- Not binding factory to JNDI, no JNDI name configured 
- Using dialect: org.hibernate.dialect.SybaseDialect 
- processing one-to-many association mappings 
- processing one-to-one association property references 
- processing foreign key constraints 
- processing one-to-many association mappings 
- processing one-to-one association property references 
- processing foreign key constraints 
- Running hbm2ddl schema export 
- exporting generated schema to database 
- Using Hibernate built-in connection pool (not for production use!) 
- Hibernate connection pool size: 10 
- autocommit mode: false 
- using driver: com.sybase.jdbc3.jdbc.SybDriver at URL: jdbc:sybase:Tds:172.22.80.78:5000/caiwu 
- connection properties: {user=sa, password=****} 
- schema export complete 
- SQL Warning: 4017, SQLState: 01ZZZ 
- Neither language name in login record 'chinese' nor language name in syslogins ' <NULL>' is an official language name on this SQL Server.  Using server-wide default 'us_english' instead. - cleaning up connection pool: jdbc:sybase:Tds:172.22.80.78:5000/caiwu 
- Checking 0 named queries 
- cleaning up connection pool: jdbc:sybase:Tds:172.22.80.78:5000/caiwu Process finished with exit code 0 请问这是哪里出了问题??????先谢了 

解决方案 »

  1.   

    你没开启事务:这样试一下:
     Configuration con = new Configuration().configure(); 
        SessionFactory sessionFactory = con.buildSessionFactory();
        
        User user = new User(); 
        Session session = sessionFactory.openSession(); 
        Transaction tx = session.beginTransaction();    user.setId(100); 
        user.setName("chenaaaaaaaa"); 
        user.setPassword("123456"); 
        session.save(user); 
        tx.commit();
        session.close(); 
      

  2.   

    - Configured SessionFactory: null