1. 环境:
Hibernate版本:hibernate3.jar
db2版本:DB2 v9.1 .200.166
2. hibernate.cfg.xml配置文件
<! DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
< hibernate-configuration >
< session-factory >
< property name ="hibernate.show_sql" > true </ property >
<!-- DB2 -->
< property name ="connection.driver_class" > com.ibm.db2.jcc.DB2Driver </ property >
< property name ="connection.username" > db2admin </ property >
< property name ="connection.password" > db2admin </ property >
< property name ="connection.url" > jdbc:db2://192.168.1.16:50000/sample </ property >
< property name ="default_schema" > administrator </ property >
< property name ="show_sql" > true </ property >
< property name ="dialect" > org.hibernate.dialect.DB2Dialect </ property >
</ session-factory >
</ hibernate-configuration >
3. 测试类别:TestCase
Logger logger = Logger.getLogger(HibernateTest.class);
private Session session = null;
public HibernateTest(String arg0) ...{
super(arg0);
}
/**//* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception ...{
try ...{
System.err.println("开启HibernateSession.....");
session = new Configuration().configure().buildSessionFactory().openSession();
System.err.println("连接成功.....");
}
catch (Exception e) ...{
e.printStackTrace();
logger.error(e);
}
}
/**//* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception ...{
if (session != null) ...{
session.close();
}
}
/**//**
* 测试Hibernate新增
*/
public void testInsert() ...{
//TODO
}
}
结果:
开启HibernateSession.....
00:26:54,296 INFO Environment:464 - Hibernate 3.0.5
00:26:54,312 INFO Environment:477 - hibernate.properties not found
00:26:54,328 INFO Environment:510 - using CGLIB reflection optimizer
00:26:54,328 INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
00:26:54,453 INFO Configuration:1110 - configuring from resource: /hibernate.cfg.xml
00:26:54,453 INFO Configuration:1081 - Configuration resource: /hibernate.cfg.xml
00:26:54,687 INFO Configuration:1222 - Configured SessionFactory: null
00:26:54,703 INFO Configuration:875 - processing extends queue
00:26:54,703 INFO Configuration:879 - processing collection mappings
00:26:54,703 INFO Configuration:888 - processing association property references
00:26:54,703 INFO Configuration:917 - processing foreign key constraints
00:26:54,718 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
00:26:54,718 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
00:26:54,718 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
00:26:55,281 INFO DriverManagerConnectionProvider:80 - using driver: com.ibm.db2.jcc.DB2Driver at URL: jdbc:db2://192.168.1.16:50000/sample
00:26:55,281 INFO DriverManagerConnectionProvider:86 - connection properties: {user=db2admin, password=****}
00:26:56,343 INFO SettingsFactory:77 - RDBMS: DB2/NT, version: SQL09012
00:26:56,343 INFO SettingsFactory:78 - JDBC driver: IBM DB2 JDBC Universal Driver Architecture, version: 3.3.54
00:26:56,390 INFO Dialect:92 - Using dialect: org.hibernate.dialect.DB2Dialect
00:26:56,500 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
00:26:56,500 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
00:26:56,500 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
00:26:56,500 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
00:26:56,500 INFO SettingsFactory:144 - Scrollable result sets: enabled
00:26:56,500 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
00:26:56,515 INFO SettingsFactory:160 - Connection release mode: null
00:26:56,515 INFO SettingsFactory:178 - Default schema: administrator
00:26:56,515 INFO SettingsFactory:187 - Default batch fetch size: 1
00:26:56,515 INFO SettingsFactory:191 - Generate SQL with comments: disabled
00:26:56,515 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
00:26:56,515 INFO SettingsFactory:334 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
00:26:56,515 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
00:26:56,515 INFO SettingsFactory:203 - Query language substitutions: {}
00:26:56,515 INFO SettingsFactory:209 - Second-level cache: enabled
00:26:56,515 INFO SettingsFactory:213 - Query cache: disabled
00:26:56,531 INFO SettingsFactory:321 - Cache provider: org.hibernate.cache.EhCacheProvider
00:26:56,531 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
00:26:56,531 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
00:26:56,546 INFO SettingsFactory:257 - Echoing all SQL to stdout
00:26:56,546 INFO SettingsFactory:261 - Statistics: disabled
00:26:56,546 INFO SettingsFactory:265 - Deleted entity synthetic identifier rollback: disabled
00:26:56,546 INFO SettingsFactory:279 - Default entity-mode: pojo
00:26:56,781 INFO SessionFactoryImpl:152 - building session factory
00:26:56,796 WARN Configurator:125 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/Java/10%20-%20Spring/SpringDemo2/MyFramework/WebContent/WEB-INF/lib/ehcache-0.9.jar!/ehcache-failsafe.xml
00:26:56,828 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
00:26:56,843 INFO SessionFactoryImpl:379 - Checking 0 named queries
连接成功.....
4. 必须添加的Jar包
1)db2 9.0 的驱动jar包:db2java.zip db2jcc.jar db2jcc_license_cu.jar 可以从%db2_home%SQLLIB/java中获取
驱动不匹配对应的讯息
DatabaseMetaData information is not known for server DB2/NTSQL09012 by this version of JDBC driver
2)dom4j-1.4.jar 缺少该包对应的Log讯息
java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader
at org.hibernate.util.XMLHelper.createSAXReader(XMLHelper.java:35)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1168)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at com.vs.test.HibernateTest.setUp(HibernateTest.java:32)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
3) ehcache-0.9.jar cglib-full-2.0.2.jar commons-collections-2.1.1.jar
如果缺少上述底包对应的Log讯息
java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.hibernate.cfg.SettingsFactory.createCacheProvider(SettingsFactory.java:323)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:219)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1463)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1004)
at com.vs.test.HibernateTest.setUp(HibernateTest.java:32)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
java.lang.NoClassDefFoundError: net/sf/cglib/core/KeyFactory
at org.hibernate.impl.SessionFactoryImpl.<clinit>(SessionFactoryImpl.java:321)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1005)
at com.vs.test.HibernateTest.setUp(HibernateTest.java:32)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
java.lang.NoClassDefFoundError: org/apache/commons/collections/ReferenceMap
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1005)
at com.vs.test.HibernateTest.setUp(HibernateTest.java:32)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)