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.   

    Session session=sf.openSession();-->Session session=sf.getSession();
      

  2.   

    可以参照楼上的,尽量使用SessionFactory 的 getCurrentsession()方法,另外是不是c3p0的配置有问题啊。你再仔细查查。
      

  3.   

    自动提交为false
    还有,不知道你UserDaoHibernateImpl中update方法怎么写的
      

  4.   

    autocommit mode: false 自动提交没开 这个是关键
    load方法 用了 前面的NEW user 就是多余的
      

  5.   

    我按这种方式写 结果是Eclipse不认后者 报红叉 是不是jar包的问题啊