关于Jboss整合hibernate的疑问(支持JTA)

09-05-06 jacal

看了网上的介绍,如何将jboss整合支持hibernate的文章,看完后还是有很多疑问,不知道该如何去做(小弟初次搞hibernate,只是会简单的一些操作)。
先讲一下我的实现目的和实现方式:
目的:建立jboss环境支持hibernate和JTA,hibernate支持Annotation,并采用pico的IOC模式。
实现方式:
一、建立jboss部署hibernate配置
在jboss\server\default\deploy建立了一个jboss_hibernate.har目录,在该目录下新建一个META-INF文件夹并建立hibernate-service.xml文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<server>
<!--
A deployer service for hibernate JNDI.
-->
<mbean code=
"org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate">
<attribute name=
"DatasourceName">java:/ecampusDS</attribute>
<attribute name=
"SessionFactoryName">java:/hibernate/SessionFactory</attribute>
<attribute name=
"Dialect">org.hibernate.dialect.Oracle9Dialect</attribute>
<attribute name=
"CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>
<attribute name=
"Hbm2ddlAuto">update</attribute>
</mbean>
</server>

二、建立oracle-ds.xml
在jboss\server\default\deploy下建立oracle-ds.xml,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>ecampusDS</jndi-name>
<connection-url>jdbc:oracle:thin:@localhost:1521:oracle9i</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>ecampus</user-name>
<password>ecampus</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

三、配置hibernate项目相关配置文件hibernate.cfg.xml,内容如下:

<?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">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name=
"show_sql">true</property>
<property name=
"connection.datasource">java:/ecampusDS</property>
<property name=
"connection.username">ecampus</property>
<property name=
"connection.password">ecampus</property>
<property name=
"dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name=
"transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name=
"transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<mapping class=
"com.ecampus.model.Student" />
</session-factory>
</hibernate-configuration>

jacal
2009-05-06 11:42

四、项目调用的java类设计
项目中调用的java类我采用的是jdon框架中的com.jdon.persistence.hibernate包,其中AnnotationConfFactory.java内容如下:

package com.jdon.persistence.hibernate;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.cfg.*;
import org.hibernate.cfg.Configuration;



public class AnnotationConfFactory extends ConfFactory {
private final static Logger logger = Logger.getLogger(AnnotationConfFactory.class);

public AnnotationConfFactory(String hibernate_cfg_xml) {
super(hibernate_cfg_xml);
}

public void createSessionFactory() {

try {
Configuration configuration = null;
if ((hibernate_cfg_xml != null) && (hibernate_cfg_xml.length() != 0)){
configuration = new AnnotationConfiguration().configure(hibernate_cfg_xml);
}else{
configuration = new AnnotationConfiguration().configure();
}


this.sessionFactory = configuration.buildSessionFactory();
} catch (HibernateException e) {
logger.error("Hibernate Annotation start error: " + e);
}
/*
try{
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
this.sessionFactory = (org.hibernate.SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
}catch (Exception ex){
logger.error("Hibernate Annotation start error: " + ex);
}
*/


}
}

然后开始调试运行,启动jboss过程中没有发现问题,提示都正常,没有异常,如:
11:04:00,859 INFO [DatasourceConnectionProvider] Using datasource: java:/ecampusDS
11:04:00,859 INFO [SettingsFactory] RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
11:04:00,859 INFO [SettingsFactory] JDBC driver: Oracle JDBC driver, version: 10.1.0.2.0
11:04:00,859 INFO [Dialect] Using dialect: org.hibernate.dialect.Oracle9Dialect
11:04:00,859 WARN [Oracle9Dialect] The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead
11:04:00,859 INFO [TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.JTATransactionFactory
11:04:00,859 INFO [NamingHelper] JNDI InitialContext properties:{}
11:04:00,859 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
11:04:00,859 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
11:04:00,859 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
11:04:00,859 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
...
org.hibernate.cache.HashtableCacheProvider
11:04:00,875 INFO [SessionFactoryImpl] building session factory
...
11:04:00,875 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
...
11:04:00,875 INFO [NamingHelper] JNDI InitialContext properties:{}
11:04:00,875 INFO [Hibernate] SessionFactory successfully built and bound into JNDI [java:/hibernate/SessionFactory]

但执行数据操作的时候总是出错:
2009-05-06 10:50:57,343 [http-127.0.0.1-8080-1] ERROR com.jdon.persistence.hibernate.AnnotationConfFactory - Hibernate Annotation start error: org.hibernate.HibernateException: Could not find datasource: java:/ecampusDS
不知道如何解决。
主要有这样几个疑问:
1.网上说需要将mapping也做到hibernate部署的har项目中,该如何做呢?
2.hibernate由jboss托管,那么hibernate.cfg.xml配置文件有作用没?如果在com.jdon.persistence.hibernate.AnnotationConfFactory中采用

try{
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
this.sessionFactory = (org.hibernate.SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
}catch (Exception ex){
logger.error(
"Hibernate Annotation start error: " + ex);
}

就不报上面那个错误,但数据库操作仍然错误,没有异常信息(可能是我没有监测到)。
3.如果需要JTA支持,该如何配置呢?实在hibernate.cfg.xml中配置还是在hibernate-service.xml中配置,如何配置?

都构架不是很熟悉,问题很多,希望大家能帮忙,十分感谢!

jacal
2009-05-07 00:55

唉,自己先顶一下!

好像是jboss-hibernate.har中用来加载jboss-service.xml的hibernate类是jboss4下server/deploy/libz中的hibernate,而应用中调用的是app.war/WEB-INF/lib/下的hibernate类。
我只好将我项目中的hibernate的jar去掉,使用jboss自带的,但却出现了:
HibernateException: /hibernate.cfg.xml not found
的异常。也就是说jboss自带的hibernate jar包找不到项目/WEB-INF/classes/hibernate.cfg.xml,那么该怎么配置呢?我把绝对路径都加上了还是不行,唉!!

大家帮帮忙,先谢了!

banq
2009-05-07 10:07

Jboss 4版本以后已经自然整合了Hibernate了啊,直接配置Hibernate使用JBoss JTA就可以了

jacal
2009-05-07 11:32

谢谢banq回复!
建立jboss_hibernate.har加载hibernate是必须的吧,那么AnnotationConfFactory构造函数中提供的配置文件应该给哪个配置文件?

2Go 1 2 下一页