項目以前在 Tomcat 環境下一直都正常運行,今天應客戶要求須要遷移到 webLogic 10.3.6 下, 部署後居然拋出了 org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken 異常,通過一番搜索後弄明白了問題的產生緣由及解決方法。html
Hibernate3 採用新的基於 antlr 的 HQL/SQL 查詢翻譯器,在 hibernate3 中須要用到 antlr,然而這個包在 weblogic 中已經包含了 antrl 類庫,因此會產生一些類加載的錯誤,沒法找到在 war 或 ear 中的 hibernate3.jar。
java
(1)使用 Hibernate3 的查詢翻譯器:
hibernate.query.factory_class= org.hibernate.hql.ast.ASTQueryTranslatorFactory
web
(2)使用 Hibernate2 的查詢翻譯器:
hibernate.query.factory_class= org.hibernate.hql.classic.ClassicQueryTranslatorFactory
oracle
說明:爲了使用 Hibernate3 的批量更新和刪除功能,只能選擇(1),不然不能解釋批量更新的語句,但使用(1)時查詢條件不支持中文。若是使用(2),能夠支持中文,但無法解釋批量更新語句了app
在項目的 WEB-INF 目錄下新建 weblogic.xml,而後將 <prefer-web-inf-classes> 的值設爲 true。dom
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.3/weblogic-web-app.xsd"> <container-descriptor> <!--優先加載位於 Web 應用程序的 WEB-INF 目錄中的類,而後再加載應用程序或系統類加載器中的類--> <prefer-web-inf-classes>false</prefer-web-inf-classes> </container-descriptor> </weblogic-web-app>
說明:若是恰好你的應用使用了 CXF 發佈 webService,那麼使用該方案後會出現 javax/xml/namespace/QName 沒法識別的異常,大概錯誤信息以下:webapp
Invocation of init method failed; nested exception is java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME" the class loader (instance of weblogic/utils/classloaders/ChangeAwareClassLoader) of the referring class, javax/xml/datatype/DatatypeConstants, and the class loader (instance of <bootloader>) for the field's resolved type, javax/xml/namespace/QName, have different Class objects for that type
該問題如何解決將在後面的方案三和方案四時提到。
ide
修改 %DOMAIN_HOME%/bin/setDomainEnv.cmd(Linux 爲 setDomainEnv.sh),如:D:\Program\weblogic-10.3.6\mydomain\bin\setDomainEnv.cmd,在 set JAVA_HOME 的後面加上set PRE_CLASSPATH=path_of_antlr_jar,如:spa
set PRE_CLASSPATH=F:\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar
修改 WEB-INF/weblogic.xml,很少做說明,直接上代碼(此處的 prefer-application-resources 是爲了解決方案二中提到的 CXF 發佈 webService 的問題):.net
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.3/weblogic-web-app.xsd"> <container-descriptor> <prefer-web-inf-classes>false</prefer-web-inf-classes> <prefer-application-resources> <resource-name>META-INF/services/javax.xml.ws.spi.Provider</resource-name> </prefer-application-resources> <prefer-application-packages> <package-name>antlr.*</package-name> </prefer-application-packages> </container-descriptor> </weblogic-web-app>
附:weblogic.xml 部署描述符參考文檔
http://docs.oracle.com/cd/E24329_01/web.1211/e21049/weblogic_xml.htm
http://edocs.weblogicfans.net/wls/docs92/webapp/weblogic_xml.html
參考資料:http://guojuanjun.blog.51cto.com/277646/288121http://tobato.iteye.com/blog/1845969http://stackoverflow.com/questions/2702266/classnotfoundexception-hqltoken-when-running-in-weblogic