public static void main(String[] args) {
// TODO Auto-generated method stub
UserDaoHibernateImpl userDaoImp=new UserDaoHibernateImpl();
String userName="sa";
String password="sa";
User user=new User();
user.setId(Long.valueOf(111));
user.setUserName(userName);
user.setPassword(password);
user=userDaoImp.load(SessionFacotry.getSession(), 1L);
System.out.println(user.getUserName()+"\t"+user.getPassword()+"\t"+user.getId());
user.setPassword("sapassword");
userDaoImp.update(user, SessionFacotry.getSession());
System.out.println(user.getUserName()+"\t"+user.getPassword()+"\t"+user.getId());
}在单元测试执行的时候报如下提示,不能修改数据值.
2010-11-4 21:40:50 org.hibernate.cfg.Environment <clinit>
信息: Hibernate 3.2.6
2010-11-4 21:40:50 org.hibernate.cfg.Environment <clinit>
信息: loaded properties from resource hibernate.properties: {hibernate.c3p0.timeout=1800, hibernate.connection.driver_class=net.sourceforge.jtds.jdbc.Driver, hibernate.c3p0.max_statements=50, hibernate.dialect=org.hibernate.dialect.SQLServerDialect, hibernate.c3p0.max_size=20, hibernate.c3p0.min_size=5, hibernate.connection.username=sa, hibernate.connection.url=jdbc:jtds:sqlserver://localhost:1433/test, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****}
2010-11-4 21:40:50 org.hibernate.cfg.Environment buildBytecodeProvider
信息: Bytecode provider name : cglib
2010-11-4 21:40:50 org.hibernate.cfg.Environment <clinit>
信息: using JDK 1.4 java.sql.Timestamp handling
2010-11-4 21:40:50 org.hibernate.cfg.Configuration configure
信息: configuring from resource: /hibernate.cfg.xml
2010-11-4 21:40:50 org.hibernate.cfg.Configuration getConfigurationInputStream
信息: Configuration resource: /hibernate.cfg.xml
2010-11-4 21:40:50 org.hibernate.cfg.Configuration addResource
信息: Reading mappings from resource : com/magtech/model/User.hbm.xml
2010-11-4 21:40:50 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
信息: Mapping class: com.magtech.model.User -> users
2010-11-4 21:40:50 org.hibernate.cfg.Configuration doConfigure
信息: Configured SessionFactory: null
2010-11-4 21:40:50 org.hibernate.connection.C3P0ConnectionProvider configure
信息: C3P0 using driver: net.sourceforge.jtds.jdbc.Driver at URL: jdbc:jtds:sqlserver://localhost:1433/test
2010-11-4 21:40:50 org.hibernate.connection.C3P0ConnectionProvider configure
信息: Connection properties: {user=sa, password=****}
2010-11-4 21:40:50 org.hibernate.connection.C3P0ConnectionProvider configure
信息: autocommit mode: false
2010-11-4 21:40:50 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
2010-11-4 21:40:50 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
2010-11-4 21:40:50 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@f28fe794 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@f1d10b56 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge0yx8c6ex840yrf1zu|73a34b, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@7ceb59de [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hge0yx8c6ex840yrf1zu|18dfef8, jdbcUrl -> jdbc:jtds:sqlserver://localhost:1433/test, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hge0yx8c6ex840yrf1zu|5e179a, numHelperThreads -> 3 ]
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: RDBMS: Microsoft SQL Server, version: 09.00.1399
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: JDBC driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase, version: 1.2
2010-11-4 21:40:51 org.hibernate.dialect.Dialect <init>
信息: Using dialect: org.hibernate.dialect.SQLServerDialect
2010-11-4 21:40:51 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
信息: Using default transaction strategy (direct JDBC transactions)
2010-11-4 21:40:51 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
信息: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Automatic flush during beforeCompletion(): disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Automatic session close at end of transaction: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Scrollable result sets: enabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: JDBC3 getGeneratedKeys(): enabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Connection release mode: auto
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Default batch fetch size: 1
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Generate SQL with comments: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Order SQL updates by primary key: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Order SQL inserts for batching: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
信息: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2010-11-4 21:40:51 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
信息: Using ASTQueryTranslatorFactory
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Query language substitutions: {}
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: JPA-QL strict compliance: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Second-level cache: enabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Query cache: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory createCacheProvider
信息: Cache provider: org.hibernate.cache.NoCacheProvider
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Optimize cache for minimal puts: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Structured second-level cache entries: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Statistics: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Deleted entity synthetic identifier rollback: disabled
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Default entity-mode: pojo
2010-11-4 21:40:51 org.hibernate.cfg.SettingsFactory buildSettings
信息: Named query checking : enabled
2010-11-4 21:40:51 org.hibernate.impl.SessionFactoryImpl <init>
信息: building session factory
2010-11-4 21:40:51 org.hibernate.impl.SessionFactoryObjectFactory addInstance
信息: Not binding factory to JNDI, no JNDI name configured
2010-11-4 21:40:51 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
信息: Running hbm2ddl schema update
2010-11-4 21:40:51 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
信息: fetching database metadata
2010-11-4 21:40:51 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
信息: updating schema
2010-11-4 21:40:51 org.hibernate.tool.hbm2ddl.TableMetadata <init>
信息: table found: test.dbo.users
2010-11-4 21:40:51 org.hibernate.tool.hbm2ddl.TableMetadata <init>
信息: columns: [id, username, password]
2010-11-4 21:40:51 org.hibernate.tool.hbm2ddl.TableMetadata <init>
信息: foreign keys: []
2010-11-4 21:40:51 org.hibernate.tool.hbm2ddl.TableMetadata <init>
信息: indexes: [pk_users]
2010-11-4 21:40:51 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
信息: schema update complete
sa sa 1----------------------------------------
进入的jar包是
ant-antlr-1.6.3.jar,asm.jar,asm-attrs.jar,cglib-2.1.jar,commons-collections-2.1.1,jar
commons-logging-1.1.1.jar,dom4j-1.6.jar,ehcache-1.2.4.jar,hibernate3.jar,jta-1.1.jar
c3p0-0.9.1.jar
----------------------------
public class SessionFacotry {
 public static Session getSession(){
  Configuration conf=new Configuration().configure();
  SessionFactory sf=conf.buildSessionFactory();
  Session session=sf.openSession();
  return session;
 }

解决方案 »

  1.   

    这是Log4j的日志信息,myeclipse报的错最好贴出来
      

  2.   

     autocommit mode: false 你没设置自动提交
    还有你的测试方法很有问题 
     
    User user=new User();
            user.setId(Long.valueOf(111));
            user.setUserName(userName);
            user.setPassword(password);
            user=userDaoImp.load(SessionFacotry.getSession(), 1L);
            System.out.println(user.getUserName()+"\t"+user.getPassword()+"\t"+user.getId());
            user.setPassword("sapassword");
            userDaoImp.update(user, SessionFacotry.getSession());你NEW 了一个user 可是又自己load了一个对象 前面等于就没NEW
    load的时候可能会有异常 
    你先把自动提交设置再看吧
     
      

  3.   


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory>
    <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433/test</property>
            <property name="hibernate.connection.username">sa</property>
            <property name="hibernate.connection.password">123456</property>
            <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
            <property name="hibernate.c3p0.min_size">5</property>
            <property name="hibernate.c3p0.max_size">20</property>
            <property name="hibernate.c3p0.timeout">1800</property>
            <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="autocommit mode">true</property>
    <mapping resource="com/magtech/model/User.hbm.xml"/>
     </session-factory>

    </hibernate-configuration>这是xml中的内容
      

  4.   

    <property name="hibernate.connection.autocommit">true</property>
      

  5.   

    还有,建议你加设一个hibernate.show_sql属性为true到上述配置文件中,以观察对数据库的动作。
      

  6.   

            user=userDaoImp.load(SessionFacotry.getSession(), 1L);
            System.out.println(user.getUserName()+"\t"+user.getPassword()+"\t"+user.getId());
            user.setPassword("sapassword");
            userDaoImp.update(user, SessionFacotry.getSession());
    user=userDaoImp.load(SessionFacoty.getSession().1L);
    把这个方法改成get试试
      

  7.   

    我加上了 果然update语句没有显示出来啊,哪的原因呢啊?
      

  8.   

    如果要做事务,最好得到Transaction对象。开始事务,提交事务,回滚事务。