我花了相当多的时间研究并试图弄清楚我的配置出了什么问题,但我完全卡住了。从帖子我一直在读的暗示是我的驱动程序不在类路径中。我添加了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();
的hibernate.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>
的persistence.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核心,4.1.2.Final.jar
||| -HIBERNATE-的EntityManager,4.1.2.Final.jar
||| -HIBERNATE-JPA-2.0-API-1.0.1.Final.jar
答案 0 :(得分:1)
我不认为我提到过我正在使用JBoss7.1。最后,我无法弄清楚为什么Hibernate无法加载驱动程序但是通过切换到使用JNDI并在JBoss standalone.xml中配置数据源而不是尝试从Hibernate建立连接来解决它。
答案 1 :(得分:0)
我认为可能是因为hibernate.cfg.xml位置错误。您应该检查它是否在类路径中,或者您可以尝试在方法调用中添加类路径。
SessionFactory sessionFactory = new Configuration().configure(
"/com/mkyong/persistence/hibernate.cfg.xml")
.buildSessionFactory();