今日经过几个小时的测试与调试,成功完成Hibernate调用Weblogic的数据源,其实不是什么大问题,如果有Spirng来做这事,就简单多了,现在的项目没有利用Spring,而是Struts + Hibernate,在weblogic上运行,不罗嗦了,具体步骤如下:
一、安装weblogic,配置数据库连接池和数据源。我使用的是SqlServer 2000 ,数据源名称为myDataSource;
二、Hibernate的配置文件如下:
<?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.datasource">myDataSource</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.DatasourceConnectionProvider</property>
<property name="hibernate.jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.jdbc.fetch_size">100</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="zx/common/model/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
注意: 在运行过程中出现ClassNotFoundException: org.hibernate.hql.ast.HqlToken,则配置hibernate.query.factory_class就可以解决了.
三、相关持久类、映射文件 省略
四、调用
//SessionFactory sessions = new Configuration().buildSessionFactory();
SessionFactory sessions = new Configuration().configure().buildSessionFactory();
Session session = sessions.openSession();
Query query = session.createQuery(" from User as t ");
List list = query.list();
五、一切ok!