问题
I'm creating OSGi dynamic bundle which should communicate with database and I am using Hibernate as I used it in non OSGI application. I putted original hibernate jars in the OSGi project's lib dir, and make sure those jars are on the project's build path and runtime class-path, I copied all configuration files(i. e. hibernate-cfg.xml) in the root of the OSGi bundled jar. When I execute my bundle (JAR) inside OSGi container it throws error for not finding hibernate-cfg.xml file.
Maybe somebody knows good example how to do that?
At the moment I'm getting following error
2013-10-14 14:56:10 ERROR HibernateUtil:41 - SessionFactory creation failed:org.hibernate.HibernateException: /hibernate.cfg.xml not found
Thank you in advance
回答1:
Please folows step:
Download Hibernate zip file at hibernate.org (Now Hibernate4.3.0Final), but i test Ok with Hibernate 4.2.8Final
Create Project A as osgi project: (Project A will contain Hibernate osgi loader, and hibernate Library, have no your Entities class, and have no hibernate.cfg.xml) Extract hibernate-release-4.2.8.Final.zip to directory, copy package source (org....) in \hibernate-release-4.2.8.Final\project\hibernate-osgi\src\main\java into src of Project A.
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',
Set Bundle Classpath: (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
Set Export packages:
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
==> Now ready Project A (Osgi project).
Create Osgi Project B, in Project B with have your Entity class, file hibernate.cfg.xml , Library for Jdbc driver you can declare in B osgi.
In B Activator:
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(); } }
来源:https://stackoverflow.com/questions/19357487/hibernate-and-osgi-integration-it-unable-to-load-hibernate-configuration-file