我花了相当多的时间研究并试图找出我的配置哪里出了问题,但完全卡住了。从我一直在阅读的帖子中可以看出,我的驱动程序不在类路径中。我添加了 Class.forName("com.ibm.db2.jcc.DB2Driver") 行以确保驱动程序可用并在该行中加载。对于我可能错过的任何建议,我将不胜感激。
我的代码是:
public class TestHibernateConfig {
private static SessionFactory sessionFactory;
public static void testConfig() {
try {
DB2Driver drvr = (DB2Driver) Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
sessionFactory = cfg.buildSessionFactory();
Session sess = sessionFactory.getCurrentSession();
Transaction tx = sess.beginTransaction();
Criteria crit = sess.createCriteria(ItemData.class);
List items = crit.list();
} catch (Exception e) {
Logger logger = Logger.getLogger("");
logger.error(e.getMessage());
}
}
}
当我的代码命中时,我得到类未找到错误: sessionFactory = cfg.buildSessionFactory();
休眠.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="speed2db2">
<property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="hibernate.connection.url">jdbc:db2://appdb:50000/MYTEST</property>
<property name="hibernate.connection.username">myUser</property>
<property name="hibernate.connection.password">myPass</property>
</session-factory>
</hibernate-configuration>
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="speedPersistUnit">
<class>ItemData</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
<property name="hibernate.connection.url" value="jdbc:db2://appdb:50000/MYTEST"/>
<property name="hibernate.default_schema" value="MYSCHEMA"/>
<property name="hibernate.connection.username" value="myUser"/>
<property name="hibernate.connection.password" value="myPass"/>
<property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver"/>
</properties>
</persistence-unit>
</persistence>
从 Eclipse 中,我可以连接到数据库,如果我进入 Hibernate 透视图,我可以浏览数据库模式。我还可以使用 HQL 编辑器在我的班级中加载数据。
编辑:
09:35:50,338 INFO [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000043: Configuring from resource: /com/newpig/speed2DB2/hibernate.cfg.xml
09:35:50,340 INFO [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000040: Configuration resource: /com/newpig/speed2DB2/hibernate.cfg.xml
09:35:50,369 INFO [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000041: Configured SessionFactory: speed2db2
09:35:52,059 INFO [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000402: Using Hibernate built-in connection pool (not for production use!)
09:36:16,856 ERROR [] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) Specified JDBC Driver com.ibm.db2.jcc.DB2Driver class not found
错误发生在这一行。
sessionFactory = cfg.buildSessionFactory();
部署到我的 .war 文件:
.War
|-WEB-INF
||-lib
|||-db2jcc4.jar
|||-hibernate-core-4.1.2.Final.jar
|||-hibernate-entitymanager-4.1。 2.Final.jar
|||-hibernate-jpa-2.0-api-1.0.1.Final.jar