請folows步:
下載Hibernate的zip文件在hibernate.org(現在Hibernate4.3.0Final), 但是我測試OK使用Hibernate 4.2.8Final
創建項目A爲OSGi項目: (A項目將包含休眠OSGi的裝載機, 和Hibernate庫,有沒有你的實體類, 並沒有hibernate.cfg .xml) 將hibernate-release-4.2.8.Final.zip 複製到目錄中,複製軟件包源代碼(org ....) \ hibernate-release-4.2.8.Final \ project \ hibernate-osgi \ src \ main \ java 轉化爲項目A的src。
Setup Activator class: (MANIFEST.MF)
Bundle-Activator: org.hibernate.osgi.HibernateBundleActivator
Create directory 'libs/hiber-4.2.8' in project A:
Copy All <DIR>\hibernate-release-4.2.8.Final\lib to your 'libs/hiber-4.2.8',
設定的批量類路徑:(MANIFEST.MF)
Bundle-ClassPath: .,
libs/hiber-4.2.8/jpa/hibernate-entitymanager-4.2.8.Final.jar,
libs/hiber-4.2.8/optional/ehcache/ehcache-core-2.4.3.jar,
libs/hiber-4.2.8/optional/ehcache/slf4j-api-1.6.1.jar,
libs/hiber-4.2.8/required/antlr-2.7.7.jar,
libs/hiber-4.2.8/required/dom4j-1.6.1.jar,
libs/hiber-4.2.8/required/hibernate-commons-annotations-4.0.2.Final.jar,
libs/hiber-4.2.8/required/hibernate-core-4.2.8.Final.jar,
libs/hiber-4.2.8/required/hibernate-jpa-2.0-api-1.0.1.Final.jar,
libs/hiber-4.2.8/required/javassist-3.18.1-GA.jar,
libs/hiber-4.2.8/required/jboss-logging-3.1.0.GA.jar,
libs/hiber-4.2.8/required/jboss-transaction-api_1.1_spec-1.0.1.Final.jar
設置導出包:
Export-Package: javax.persistence,
org.hibernate,
org.hibernate.annotations,
org.hibernate.cfg,
org.hibernate.criterion,
org.hibernate.dialect,
org.hibernate.dialect.function,
org.hibernate.exception,
org.hibernate.internal.util,
org.hibernate.jdbc,
org.hibernate.mapping,
org.hibernate.osgi,
org.hibernate.property,
org.hibernate.service,
org.hibernate.tool.hbm2ddl,
org.hibernate.type
==>現在準備項目A(OSGi的項目)。
創建OSGI項目B,在B項目與有實體類, hibernate.cfg.xml文件,庫JDBC驅動程序,你可以B中的OSGi聲明。
在B中激活:
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
this.loadOsgiHibernateService(bundleContext);
}
private void loadOsgiHibernateService(BundleContext bundleContext) {
ServiceReference<?> ref = context
.getServiceReference(SessionFactory.class.getName());
if (ref != null) {
SessionFactory factory = (SessionFactory) context.getService(ref);
// Ready session factory.
Session session= factory.getCurrentSession();
}
}
你最有可能需要使用由Hibernate提供的最新版本,導致舊版本確實有類加載器和加載資源文件的一些問題。 –
我的問題已解決,我只是用3.2.6.ga更新hibernate jar版本並開始工作。 感謝Achim Nierbeck的迴應和線索。 – anuragR